Django template and the locals trick

后端 未结 8 1438
不思量自难忘°
不思量自难忘° 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:03

    I agree with Alex. Don't see the point of creating a class instance (as niels suggested) when you can just do this:

    def my_view(request, id):
        customer = ..
        invoice = ..
        date = ..
        return render_to_response('xxx,html', locals())
    

    If you want a performance reason, dotted lookups are slower.

    If you want a maintenance reason, this is fewer lines of code, even more readable, and one fewer unnecessary structures.

提交回复
热议问题