Navigation in django

后端 未结 30 1241

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 09:33

    You could use the reverse function with the appropriate parameters to get the current url.

    0 讨论(0)
  • 2020-11-27 09:33

    from this SO Question

    {% url 'some_urlpattern_name' as url %}
    <a href="{{url}}"{% if request.path == url %} class="active"{% endif %}>Link</a>
    

    Repeat as necessary for each link.

    0 讨论(0)
  • 2020-11-27 09:35

    I use a combination of this mixin for class based views:

    class SetActiveViewMixin(object):
        def get_context_data(self, **kwargs):
            context = super(SetActiveViewMixin, self).get_context_data(**kwargs)
            context['active_nav_menu'] = {
                self.request.resolver_match.view_name: ' class="pure-menu-selected"'
            }
            return context
    

    with this in the template:

    <ul>
        <li{{active_nav_menu.node_explorer }}><a href="{% url 'node_explorer' '' %}">Explore</a></li>
        <li{{active_nav_menu.node_create }}><a href="{% url 'node_create' path %}">Create</a></li>
        <li{{active_nav_menu.node_edit }}><a href="{% url 'node_edit' path %}">Edit</a></li>
        <li{{active_nav_menu.node_delete }}><a href="{% url 'node_delete' path %}">Delete</a></li>
    </ul>
    
    0 讨论(0)
  • 2020-11-27 09:36

    I know I'm late to the party. I didn't like any of the popular solutions though:

    The block method seems wrong: I think the navigation should be self contained.

    The template_tag method seems wrong: I don't like that I have to get the url from the url-tag first. Also, I think the css-class should be defined in the template, not the tag.

    I therefore wrote a filter that doesn't have the drawbacks I described above. It returns True if a url is active and can therefore be used with {% if %}:

    {% load navigation %}
    <li{% if request|active:"home" %} class="active"{% endif %}><a href="{% url "home" %}">Home</a></li>
    

    The code:

    @register.filter(name="active")
    def active(request, url_name):
        return resolve(request.path_info).url_name == url_name
    

    Just make sure to use RequestContext on pages with navigation or to enable the request context_processor in your settings.py

    TEMPLATE_CONTEXT_PROCESSORS = (
        ...
        'django.core.context_processors.request',
    )
    
    0 讨论(0)
  • 2020-11-27 09:38

    I use template inheritance to customize navigation. For example:

    base.html

    <html>
        <head>...</head>
        <body>
            ...
            {% block nav %}
            <ul id="nav">
                <li>{% block nav-home %}<a href="{% url 'home' %}">Home</a>{% endblock %}</li>
                <li>{% block nav-about %}<a href="{% url 'about' %}">About</a>{% endblock %}</li>
                <li>{% block nav-contact %}<a href="{% url 'contact' %}">Contact</a>{% endblock %}</li>
            </ul>
            {% endblock %}
            ...
        </body>
    </html>
    

    about.html

    {% extends "base.html" %}
    
    {% block nav-about %}<strong class="nav-active">About</strong>{% endblock %}
    
    0 讨论(0)
  • 2020-11-27 09:38

    My solution was to write a simple context processor to set a variable based on the request path:

    def navigation(request):
    """
    Custom context processor to set the navigation menu pointer.
    """
    nav_pointer = ''
    if request.path == '/':
        nav_pointer = 'main'
    elif request.path.startswith('/services/'):
        nav_pointer = 'services'
    elif request.path.startswith('/other_stuff/'):
        nav_pointer = 'other_stuff'
    return {'nav_pointer': nav_pointer}
    

    (Don't forget to add your custom processor to TEMPLATE_CONTEXT_PROCESSORS in settings.py.)

    Then in the base template I use an ifequal tag per link to determine whether to append the "active" class. Granted this approach is strictly limited to the flexibility of your path structure, but it works for my relatively modest deployment.

    0 讨论(0)
提交回复
热议问题