#5617 closed New feature (wontfix)
Default server_error view should render with RequestContext
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Release blocker | Keywords: | server_error, RequestContext, error, templates |
Cc: | ionel.mc@… | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Currently, the default server_error view in django.views.defaults renders using the following code:
return http.HttpResponseServerError(t.render(Context({})))
However, this prevents the context processors specified in SETTINGS.TEMPLATE_CONTEXT_PROCESSORS from being loaded. For example, if a base.html might include branding that includes the current date and time (which is passed in through a custom context processor). Currently, if we try to extend base.html in the 500.html template, we get an error because the context variable for the date and time isn't loaded.
The best solution seems to be similar to the way the page_not_found view renders:
return http.HttpResponseServerError(t.render(RequestContext(request)))
Although the page_not_found view also passes in a request_path variable to RequestContext, I'm not sure if this is necessary.
Attachments (2)
Change History (11)
by , 17 years ago
Attachment: | server_error.patch added |
---|
comment:1 by , 17 years ago
Has patch: | set |
---|
comment:2 by , 17 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
The default server-error handler was deliberately written this way to lessen the odds of new exceptions being raised during the execution of the server_error
view.
comment:3 by , 16 years ago
what about something like this?
def render_safe(request, template_name): t = loader.get_template(template_name) try: context = RequestContext(request, {'request_path': request.path}) return t.render(context) except: return t.render(Context({})) def page_not_found(request, template_name='404.html'): """ Default 404 handler. Templates: `404.html` Context: request_path The path of the requested URL (e.g., '/app/pages/bad_page/') """ return http.HttpResponseNotFound(render_safe(request, template_name)) def server_error(request, template_name='500.html'): """ 500 error handler. Templates: `500.html` Context: None """ return http.HttpResponseServerError(render_safe(request, template_name))
comment:4 by , 16 years ago
I would like to second this. I find tobias's approach best, as it doesn't break the handler on error and still allows people to use the RequestContext. I think that every site that uses MEDIA_URL will have a hacky handler500 to pass this to the template, and I think it would be much better if this was default Django bejaviour.
comment:5 by , 15 years ago
Cc: | added |
---|---|
Resolution: | wontfix |
Status: | closed → reopened |
I'm seconding this too. I know this is a django design choice but it's very inflexible - please review again.
comment:6 by , 15 years ago
Resolution: | → wontfix |
---|---|
Status: | reopened → closed |
This ticket was closed by a core developer, if you want to reopen the discussion about this please do it on the django-developers mailing list.
comment:7 by , 13 years ago
Easy pickings: | unset |
---|---|
Severity: | → Normal |
Type: | → Uncategorized |
UI/UX: | unset |
See also this discussion - https://groups.google.com/d/topic/django-developers/UH8ehYL8Pss/discussion
comment:8 by , 13 years ago
Component: | HTTP handling → ORM aggregation |
---|---|
Severity: | Normal → Release blocker |
Triage Stage: | Unreviewed → Accepted |
Type: | Uncategorized → New feature |
Replying to Nick Fishman <kwlogical@…>:
Currently, the default server_error view in django.views.defaults renders using the following code:
return http.HttpResponseServerError(t.render(Context({})))However, this prevents the context processors specified in SETTINGS.TEMPLATE_CONTEXT_PROCESSORS from being loaded. For example, if a base.html might include branding that includes the current date and time (which is passed in through a custom context processor). Currently, if we try to extend base.html in the 500.html template, we get an error because the context variable for the date and time isn't loaded.
The best solution seems to be similar to the way the page_not_found view renders:
return http.HttpResponseServerError(t.render(RequestContext(request)))Although the page_not_found view also passes in a request_path variable to RequestContext, I'm not sure if this is necessary.
comment:9 by , 12 years ago
Component: | ORM aggregation → Database layer (models, ORM) |
---|
Patch to render with RequestContext