Django custom context processor being called twice per request

我与影子孤独终老i 提交于 2019-12-08 06:35:49

问题


I created a simple custom context processor that needs to only be run once per request. After putting in some logging hooks I found that it is being called twice per request.

Is this a known "feature" that a missed in the docs? Is it related to the number of templates in the inheritance tree? Is it a bug in 1.03?


回答1:


This is not expected behavior. Context processor are executed once each time a RequestContext is instantiated). In the case of template inheritance, the same context instance is passed up to the parent template, so that shouldn't cause another execution of the context processors. Either your logging is misleading (see @Vinay Sajip's comment), or you need to figure out where in your code an extra RequestContext might be executed on each request (are you using an inclusion tag or some other custom template tag that renders a template and instantiates RequestContext?)

EDIT Sorry, by "inclusion tag" I meant (in the generic sense) some tag that renders another template, not any tag that uses the inclusion_tag decorator. A regular inclusion_tag that takes context should simply pass along the existing context object, not instantiate a new RequestContext.

One thing you could try is to place an "import pdb; pdb.set_trace()" in your context processor, run the code in the Django dev server, and in the console examine the stack trace with pdb each time your context processor gets hit, to see where it's being called from.




回答2:


In my case this bug occur when using django debug_toolbar. To avoid this try to comment

debug_toolbar.middleware.DebugToolbarMiddleware




回答3:


I figured out the issue. If a dictionary other than the original context is returned then the context processor seems to be executed again. Not sure why, and I can't be sure because I didn't look at the underlying code, but the after I updated the original context and returned that the issue went away. Thanks.




回答4:


Is this happening on a production webserver, Apache etc, or just in the built in development server? I've noticed similar behaviour locally on occassion, but I'm pretty sure it's just a quirk in the runserver.




回答5:


Hope this helps:

In my case the issue was a templatetag, namely: providers_media_js from allauth package.

Try not to return anything in your context processor and see if the issue persists. Then try to spot which variable is responsible for this problem.




回答6:


For future reference -- my problem was a render_to_string inside a view function, causing the context processor to be executed twice:

comments = render_to_string('comments.html', {'comments': comments_list}, request)

This call was cached, so it was kinda difficult to identify where the problem was. Anyway, I solved it by removing the request context from the render_to_string call, since I didn't need it for this particular case:

comments = render_to_string('comments.html', {'comments': comments_list})

Later on I refactored the code and removed the render_to_string all together, and cached the snippet directly in the template. But there are legit cases for using render_to_string inside a view function (such as rendering an email template for example), so this may cause some issues.



来源:https://stackoverflow.com/questions/1447689/django-custom-context-processor-being-called-twice-per-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!