Opened 16 years ago

Closed 14 years ago

Last modified 11 years ago

#5617 closed New feature (wontfix)

Default server_error view should render with RequestContext

Reported by: Nick Fishman <kwlogical@…> 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)

server_error.patch (579 bytes ) - added by Nick Fishman <kwlogical@…> 16 years ago.
Patch to render with RequestContext
server_error.2.patch (462 bytes ) - added by Nick Fishman <kwlogical@…> 16 years ago.
Patch with generic django path

Download all attachments as: .zip

Change History (11)

by Nick Fishman <kwlogical@…>, 16 years ago

Attachment: server_error.patch added

Patch to render with RequestContext

comment:1 by Nick Fishman <kwlogical@…>, 16 years ago

Has patch: set

by Nick Fishman <kwlogical@…>, 16 years ago

Attachment: server_error.2.patch added

Patch with generic django path

comment:2 by James Bennett, 16 years ago

Resolution: wontfix
Status: newclosed

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 Tobias McNulty, 15 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 Stavros Korokithakis, 15 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 Ionel Cristian Maries, 14 years ago

Cc: ionel.mc@… added
Resolution: wontfix
Status: closedreopened

I'm seconding this too. I know this is a django design choice but it's very inflexible - please review again.

comment:6 by Alex Gaynor, 14 years ago

Resolution: wontfix
Status: reopenedclosed

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 Tom Christie, 13 years ago

Easy pickings: unset
Severity: Normal
Type: Uncategorized
UI/UX: unset

in reply to:  description comment:8 by anonymous, 12 years ago

Component: HTTP handlingORM aggregation
Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted
Type: UncategorizedNew 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 Anssi Kääriäinen, 11 years ago

Component: ORM aggregationDatabase layer (models, ORM)
Note: See TracTickets for help on using tickets.
Back to Top