Why on earth do I have to pass RequestContext in all of my responses?

前端 未结 3 1427
灰色年华
灰色年华 2020-12-29 00:34

I want to highlight the current page in the navigation menu. Obviously I need to give the menu links a class like \'active\' when you are on their page. This is a classic pr

相关标签:
3条回答
  • 2020-12-29 00:45

    Your intention makes sense, you'll need RequestContext most of the time and only rarely it can be safely omitted for performance reasons. The solution is simple, instead of render_to_response use direct_to_template shortcut:

    from django.views.generic.simple import direct_to_template
    
    def contact(request):
        # snip ...
        return direct_to_template(request, 'contact.html', { 'myvar' : myvar })
    

    ... or render_to decorator from django-annoying:

    from annoying.decorators import render_to
    
    @render_to('template.html')
    def foo(request):          
        bar = Bar.object.all()  
        return {'bar': bar}     
    
    0 讨论(0)
  • 2020-12-29 00:45

    For future reference, one can use django-tabs for doing what OP wanted.

    0 讨论(0)
  • 2020-12-29 01:01

    You don't necessarily have to do anything to the markup of your navigation to give the current one a different style - there are declarative ways to do that using CSS.

    See my answer here: Django: Is there a better way to bold the current page link for an example.

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