Django, template context processors

前端 未结 3 695
广开言路
广开言路 2020-11-29 00:52

I have a weird problem, I want to add a global query using context processors. This is how I did it by following:

made a processor.py in my app as such:

<         


        
3条回答
  •  独厮守ぢ
    2020-11-29 01:06

    You need to add the default values of TEMPLATE_CONTEXT_PROCESSORS. However, instead of hard-coding those values, which will be tied to a specific version of Django, you can append your context processor to the default values by the following:

    from django.conf import global_settings
    TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
        "myapp.processor.foos",
    )
    

    Make sure to include the trailing comma in the tuple, so that Python recognizes it as a tuple.

提交回复
热议问题