Django template and the locals trick

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

    I guess it depends on how many local variables you define in your function.

    If it matches exactly the number you want return to your template, or the 'extra' variables are simple structures like integers or booleans, then I guess there is no point in explicitely returning them, as this requires more work.

    But on the other hand, if your view has a lot of complex 'helper' variables, like instances of your model you use in the view to generate the data you want to send to the template, then you might want to consider to use explicit variables to return to the template.

提交回复
热议问题