Navigation in django

后端 未结 30 1381

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:39

    You do not need an if to do that, have a look at the following code:

    tags.py

    @register.simple_tag
    def active(request, pattern):
        import re
        if re.search(pattern, request.path):
            return 'active'
        return ''
    

    urls.py

    urlpatterns += patterns('',
        (r'/$', view_home_method, 'home_url_name'),
        (r'/services/$', view_services_method, 'services_url_name'),
        (r'/contact/$', view_contact_method, 'contact_url_name'),
    )
    

    base.html

    {% load tags %}
    
    {% url 'home_url_name' as home %}
    {% url 'services_url_name' as services %}
    {% url 'contact_url_name' as contact %}
    
    
    

    that's it. for implementation details have a look at:
    gnuvince.wordpress.com
    110j.wordpress.com

提交回复
热议问题