Django template and the locals trick

后端 未结 8 1409
不思量自难忘°
不思量自难忘° 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})
    

提交回复
热议问题