Introduction to class-based views

Class-based views provide an alternative way to implement views as Python objects instead of functions. They do not replace function-based views, but have certain differences and advantages when compared to function-based views: Organization of code related to specific HTTP methods (GET, POST, etc.) can be addressed by separate methods instead of conditional branching. Object oriented techniques such as mixins (multiple inheritance) can be used to factor code into reusable components. The rel

JavaScript customizations in the admin

Inline form events New in Django 1.9. You may want to execute some JavaScript when an inline form is added or removed in the admin change form. The formset:added and formset:removed jQuery events allow this. The event handler is passed three arguments: event is the jQuery event. $row is the newly added (or removed) row. formsetName is the formset the row belongs to. The event is fired using the django.jQuery namespace. In your custom change_form.html template, extend the admin_change_for

Making queries

Once you’ve created your data models, Django automatically gives you a database-abstraction API that lets you create, retrieve, update and delete objects. This document explains how to use this API. Refer to the data model reference for full details of all the various model lookup options. Throughout this guide (and in the reference), we’ll refer to the following models, which comprise a Weblog application: from django.db import models class Blog(models.Model): name = models.CharField(max_

Managing files

This document describes Django’s file access APIs for files such as those uploaded by a user. The lower level APIs are general enough that you could use them for other purposes. If you want to handle “static files” (JS, CSS, etc.), see Managing static files (e.g. images, JavaScript, CSS). By default, Django stores files locally, using the MEDIA_ROOT and MEDIA_URL settings. The examples below assume that you’re using these defaults. However, Django provides ways to write custom file storage syst

Managing static files (e.g. images, JavaScript, CSS)

Websites generally need to serve additional files such as images, JavaScript, or CSS. In Django, we refer to these files as “static files”. Django provides django.contrib.staticfiles to help you manage them. This page describes how you can serve these static files. Configuring static files Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS. In your settings file, define STATIC_URL, for example: STATIC_URL = '/static/' In your templates, either hardcode the url lik

Many-to-many relationships

To define a many-to-many relationship, use ManyToManyField. In this example, an Article can be published in multiple Publication objects, and a Publication has multiple Article objects: from django.db import models class Publication(models.Model): title = models.CharField(max_length=30) def __str__(self): # __unicode__ on Python 2 return self.title class Meta: ordering = ('title',) class Article(models.Model): headline = models.CharField(max_leng

Many-to-one relationships

To define a many-to-one relationship, use ForeignKey: from django.db import models class Reporter(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30) email = models.EmailField() def __str__(self): # __unicode__ on Python 2 return "%s %s" % (self.first_name, self.last_name) class Article(models.Model): headline = models.CharField(max_length=100) pub_date = models.DateField() reporter = models.

messages.add_message()

add_message(request, level, message, extra_tags='', fail_silently=False) [source]

messages.get_messages()

get_messages(request) [source] In your template, use something like: {% if messages %} <ul class="messages"> {% for message in messages %} <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li> {% endfor %} </ul> {% endif %} If you’re using the context processor, your template should be rendered with a RequestContext. Otherwise, ensure messages is available to the template context. Even if you know there is only just one message

messages.middleware.MessageMiddleware

class MessageMiddleware [source] Enables cookie- and session-based message support. See the messages documentation.