Sunday, May 16, 2010

Django Modular Admin Part 2

I just realized that alot of the functionality that the admin login has is not part of django.contrib.auth but is only in the admin.

A couple of things I have figure out as a result with form validation errors in the template.
{{ form.non_field_errors }} is for a ValidationError that is for the whole form.

A snippet of my login.html template


<div id="welcome_login" title="Welcome To My Site"> 
  <p><b>Admin Login</b></p>
  <form action="." method="post" enctype="multipart/form-data" class="forms" name="form" >{% csrf_token %}
   {% if form.non_field_errors %}
   <div class="response-msg error ui-corner-all">
    <span>Error message</span>
    {{ form.non_field_errors }}
   </div>
   {% endif %}
     <ul> 
    <li> 
     <label for="id_username" class="desc">{% trans 'Username:' %}</label> 
     <div>
      {% if form.username.errors %}
      <div class="response-msg error ui-corner-all">
       <span>Error message</span>
       {{ form.username.errors }}
      </div>
      {% endif %}
      <input type="text" tabindex="1" maxlength="255" value="" class="field text full" name="username" id="email" />

     </div> 
    </li> 
    <li> 
     <label for="id_password" class="desc">{% trans 'Password:' %}</label>
     <div> 
      {% if form.password.errors %}
      <div class="response-msg error ui-corner-all">
       <span>Error message</span>
       {{ form.password.errors }}
      </div>
      {% endif %}
      <input type="password" tabindex="1" maxlength="255" value="" class="field text full" name="password" id="password" />
      <input type="hidden" name="this_is_the_login_form" value="1" />
     </div> 
    </li> 
   </ul> 
  </form> 
 </div>

More to come on django auth

Thursday, May 13, 2010

Modular Django Admin Part 1

The django admin is awesome, we all know that, but there is always the debate to role your own or to customize the admin. Make the admin more modular would be ideal and that is what we will be trying to do.

In part one we will just start with the basics and setup a tabular list using the object_list generic view with gives you pagination for free.

One trick to get the key/value pairs into the template of the model is using a function to return self.__dict__.iteritems()

The Model (myapp/models.py)

class MyObject(models.Model):
    name = models.CharField(max_length=75)
    slug = models.SlugField(unique=True)
    
    def __unicode__(self):
        return self.name

    def attrs(self):
        for attr, value in self.__dict__.iteritems():
            yield attr, value

The View (myapp/views.py)

from django.views.generic.list_detail import object_list
def applicant_list_jobs(request):
    return object_list(request, queryset=Job.objects.all(), paginate_by=5)

The Template (templates/myapp/myobject_list.html)

{% extends "admin/change_list.html" %}
{% block content %}
 <table>
  {% for object in object_list %}
  
  <tr class="{% cycle 'row1' 'row2' %}">
   {% for key,value in object.attrs %}
   <td>{{ value }}</td>
   {% endfor %}
  </tr>
  {% endfor %}
 </table>
 <p class="paginator">
 {% if page_obj.has_previous %}
  <a href="?page={{ page_obj.previous_page_number }}"><</a>
 {% endif %}
  Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}
 {% if page_obj.has_next %}
  <a href="?page={{ page_obj.next_page_number }}">></a>
 {% endif %}
</p>
{% endblock content %}

Next up will be where the templates and urls go for login and the built in authentication tools.