Jump to navigation

Prosthetic Conscience

Jason McBrayer's weblog; occasional personal notes and commentary

Sat, 22 Apr 2006

Some django gotchas

These are some django details I’ve found while working on my django demonstration site, things that are useful for making an integrated Django-powered site, but under-documented.

Make “View on site” work

If you give your model a “get_absolute_url” method, the admin view for each object in that model will have a “View on site” link. However, and this is the clever bit, that link will not work. I’m not exactly sure why, but instead of linking to what’s returned by “get_absolute_url”, it links to something starting with “/r/”. However, by adding this to your site’s urls.py, Django will redirect to the correct location.

    (r'^r/', include('django.conf.urls.shortcut')),

Use the admin app’s javascript widgets on your site

The admin app has a lot of neat convenience features, such as magical calendar links after each DateField. Here’s how you get those in your app.

Put this in your urls.py:

    (r'^jsi18n/$', 'django.views.i18n.javascript_catalog',
                   {'packages': 'django.conf'}), 

And put this in your base template, in the <head> section:

    <script type="text/javascript" src="/jsi18n/"></script> 
    <script type="text/javascript"

You’ll need to add some CSS styles to make things look nice; look in the admin media css folder to find what you need.

Generic views for login and logout

The Django authentication documentation is good enough to allow you to write login and logout views for your site. But in fact, there are already generic views for this purpose, which are not discussed in the generic views documentation.

Add to your urls.py:

    (r'^accounts/login/', 'django.views.auth.login.login'),
    (r'^accounts/logout/', 'django.views.auth.login.logout'),

And write templates. Mine look like this:

[Template Dir]/registration/login.html:

{% extends "base" %}
{% block title %}Site : login{% endblock %}

{% block content %}
<form action="/accounts/login/?next={{ request.GET.next }}"

      {%if form.username.errors %}
      <span style="color: red;">
        {{ form.username.errors|join:", " }}
      {% endif %}

    <label class="fortextinput" for="id\_username">
    {{ form.username }}<br/>

    {%if form.password.errors %}
    <span style="color: red;">
      {{ form.password.errors|join:", " }}
    {% endif %}

    <label class="fortextinput" for="id\_password">
    {{ form.password }}<br/>

    <input type="submit" value="Login"/>

and [Template Dir]/registration/logged_out.html :

{% extends "base" %}
{% block title %}site: logged out{% endblock %}

{% block content %}

<h1>Logged out</h1>
<p>You have logged out successfully.  This makes you special!</p>
{% endblock %}

This, with login/logout links (wrapped with if user.is_anonymous) in your templates, is enough to do basic login/logout for your entire site. Staff users will be able to use this login to get to the admin pages, as well.

[ Posted: 08:35] | [ Category: ] | Permalink | Comments: ]


Powered by PyBlosxom