Create a new module

Here are the few steps to create a django-intranet module.

Models

First of all create your models:

from django.db import models
from django.utils.translation import ugettext_lazy as _

class Category(models.Model):
    name = models.CharField(_('name'), max_length=50)

    def __unicode__(self):
        return u'%s' % self.name

    class Meta:
        verbose_name = _('category')
        ordering = ('name', )

class Prospect(models.Model):

    company_name = models.CharField(_('company name'), max_length=100)
    category = models.ForeignKey(Category, related_name="prospects")

    title = models.CharField(_('title'), max_length=5, blank=True)
    first_name = models.CharField(_('first name'), max_length=50, blank=True)
    last_name = models.CharField(_('last name'), max_length=50, blank=True)
    e_mail = models.EmailField(_('e-mail'), blank=True)
    phone_number = PhoneField(_('phone number'), blank=True)

    def __unicode__(self):
        return u'%s' % self.company_name

    class Meta:
        verbose_name = _('prospect')
        ordering = ['company_name']

URLs

You can automatically generate URLs and views for displays and treatments of your database: display of the list of your database table, display of the details of an entry, creation, modification and removal of an entry. To use this feature, you have to add the new generated URLs to your URL patterns:

from intranet.utils import get_default_model_url
from my_app.models import Category, Prospect

urlpatterns = get_default_model_url(Category)
urlpatterns += get_default_model_url(Prospect)

There are also a number of parameters you can customize:

  • form_class: a customized form view for your model
  • url_name: a string to name the URLs of your model in your application. By default it is the name of your model in lower-case.
  • prefix_pattern: a string or a regular expression to define a prefix before the URLs of your model. There is no prefix by default.
  • prefix_name: a string that will be used as a prefix before the name of your URLs (url_name). There is no prefix by default.
  • list_view, detail_view, creation_view, update_view, delete_view: customized views for respectively the display of the list of your database entries, the display of one entry, the creation, update and removal of an entry.

For example, you could define the following URL pattern:

# Import your own vues.
from views import ProspectListView, ProspectDetailView

urlpatterns = get_default_model_url(
    Prospect,
    url_name = 'pro',
    prefix_pattern = '(?P<company_id>\d+)/',
    prefix_name = 'pre-',
    list_view = ProspectListView,
    detail_view = ProspectDetailView,

The previous code is equivalent to:

from views import ProspectListView, ProspectDetailView
from django.conf.urls import patterns, url

urlpatterns = patterns(url(
    r'^(?P<company_id>\d+)/prospect/$',
    login_required(list_view.as_view(model=Prospect)),
    name = 'pre-pro-list'
))
urlpatterns = patterns(url(
    r'^(?P<company_id>\d+)/prospect/(?P<pk>\d+)/$',
    login_required(detail_view.as_view(model=Prospect)),
    name = 'pre-pro-detail'
))
urlpatterns = patterns(url(
    r'^(?P<company_id>\d+)/prospect/add/$',
    login_required(creation_view.as_view(model=Prospect)),
    name = 'pre-pro-add'
))
urlpatterns = patterns(url(
    r'^(?P<company_id>\d+)/prospect/(?P<pk>\d+)/edit/$',
    login_required(update_view.as_view(model=Prospect)),
    name = 'pre-pro-edit'
))
urlpatterns = patterns(url(
    r'^(?P<company_id>\d+)/prospect/(?P<pk>\d+)/delete/$',
    login_required(delete_view.as_view(model=Prospect)),
    name = 'pre-pro-delete'
))

Views

You can inherit from django-intranet base_views to customize your change_list display:

from intranet.base_views import IntranetListView

class ProspectListView(IntranetListView):
    list_display = ('company_name', 'last_name', 'category')
    list_display_links = ('company_name',)
    search_fields = (
        'company_name',
        'first_name',
        'last_name',
        'category__name',
    )

Templates

You will probably have to customize the model_detail template:

You can create templates/<module_name>/<model>/model_detail.html

For our app, we will create : templates/prospect/prospect/model_detail.html and templates/prospect/category/model_detail.html

{% extends 'intranet/model_detail.html' %}
{% load i18n intranet_extra intranet_values_extras %}
{% load url from future %}

{% block title_content %}{{ verbose_name|capfirst }} : {{ object|upper }}{% endblock %}

{% block content %}
<div class="row-fluid">
  <div class="span3">
    <table class="{% default_table_class %}">
      <tr><th colspan="2" style="text-align:center;">{% trans "General" %}</th></tr>
      <tr><th>{% trans "Company name" %}</th><td>{{ object.company_name|upper }}</td></tr>
      <tr><th>{% trans "Category" %}</th><td><a href="{% url 'category-detail' object.category.pk %}">{{ object.category.name|title }}</a></td></tr>
    </table>
  </div>
  <div class="span3">
    <table class="{% default_table_class %}">
      <tr><th colspan="2" style="text-align:center;">{% trans "Contacts" %}</th></tr>
      <tr><th>{% trans "Company head" %}</th><td>
        {% if object.last_name == "" %}
          {{object.first_name|title }}
        {% else %}
          {{ object.title|title }} {{object.first_name|title }} {{ object.last_name|upper }}
        {% endif %}
      </td></tr>
      <tr><th>{% trans "Phone" %}</th><td>{{ object.phone_number|phone }}</td></tr>
      <tr><th>{% trans "E-mail" %}</th><td><a href="mailto:{{ object.e_mail }}">{{ object.e_mail }}</a></td></tr>
    </table>
  </div>
</div>
{% endblock %}

Screenshots

Here are some screenshots of the templates of a project using django-intranet. We defined two models: Field and Prospect.

Display of the Field list:

Display of the 'Field' list

Display of a customized view of a Field entry:

Display of the details of a 'Field' entry

Display of the Prospect list:

Display of the 'Prospect' list

Display of the details of a Prospect entry:

Display of the details of a 'Prospect' entry

Creation of a prospect entry:

Creation of a prospect entry

Modification of a prospect entry:

Modification of a prospect entry