Django template and the locals trick

后端 未结 8 1385
不思量自难忘°
不思量自难忘° 2020-12-02 10:43

The django books gives the local trick in order to avoid to type a long list of parameters as context dictionnary

http://www.djangobook.com/en/2.0/chapter04/

相关标签:
8条回答
  • 2020-12-02 11:06

    I know this is an old thread...currently render_to_response is deprecated. Use render instead without locals(). Passing around all locals is a bad practice. Here is an views.py example:

    from django.shortcuts import render
    from django.contrib.auth.decorators import login_required
    
    @login_required
    def mybooks(request):
        entries = Book.objects.all()
        return render(request, 'mybooks.html', {'entries': entries})
    
    0 讨论(0)
  • 2020-12-02 11:14

    I don't like it, personally. There's probably no reason for my preference, other than the old Python dictum "Explicit is better than implicit". I like to know exactly what's going into my templates.

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