Home > Uncategorized > Django pro tip: if you only use the admin

Django pro tip: if you only use the admin

October 11, 2011 Leave a comment Go to comments

If you have a project that only exposes the admin you should just use the 500/404 templates from the admin.

Put this in your project’s urls.py:

from django.utils.functional import curry
from django.views.defaults import server_error, page_not_found

handler500 = curry(server_error, template_name='admin/500.html')
handler404 = curry(page_not_found, template_name='admin/404.html')

I wonder why django doesn’t mention those templates in the docs …

If you have other drop-in apps that need authentication (like rosetta or sentry) bare in mind that the admin doesn’t have a reusable login view so you must hook one. You should just reuse django admin’s login template. Put this in the urlpatterns (don’t forget to match it to LOGIN_URL in the settings):

    url(r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'admin/login.html'}),

You might note that this is not very DRY but actually the LOGIN_URL might differ than the one in the urlpatterns (eg: you mount the django wsgi handler on a non-root path).

About these ads
Tags: ,
  1. October 11, 2011 at 3:20 pm | #1

    Nice tip!. Another way to do it, if you have at least one custom app, is create both templates in your app but with {% extends “admin/404.html” %} and {% extends “admin/500.html” %} inside them
    Thanks

  2. October 11, 2011 at 7:53 pm | #2

    s/bare in mind/bear in mind/

  3. October 12, 2011 at 6:42 am | #3

    Thanks a lot. Really useful (as in I am going to use it now!)

  4. February 29, 2012 at 3:04 pm | #4

    thanks that’s pretty cool

  1. October 12, 2011 at 7:39 am | #1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 170 other followers