Navigation in django

后端 未结 30 1487

I\'ve just done my first little webapp in django and I love it. I\'m about to start on converting an old production PHP site into django and as part its template, there is a

30条回答
  •  鱼传尺愫
    2020-11-27 10:00

    Since Django 1.5:

    In all generic class-based views (or any class-based view inheriting from ContextMixin), the context dictionary contains a view variable that points to the View instance.

    So if you are using such views, you could add something likie breadcrumbs as a class level field and use it in your templates.

    Example view code:

    class YourDetailView(DetailView):
         breadcrumbs = ['detail']
         (...)
    

    In your template you could use it in this way:

    Detail
    

    If you want to additionally "highlight" parent navigation items, you need to extend breadcrumbs list:

    class YourDetailView(DetailView):
         breadcrumbs = ['dashboard', 'list', 'detail']
         (...)
    

    ... and in your template:

    Dashboard
    List
    Detail
    

    This is easy and clean solution and works pretty well with nested navigation.

提交回复
热议问题