Reference
The Django template language: for Python programmers
staticEngine.get_default()[source] : Returns the underlying Engine from the first configured DjangoTemplates engine. Raises ImproperlyConfigured if no engines are configured. Engine.from_string(template_code)[source] : Compiles the given template code and returns a Template object.
Reference
Built-in template tags and filters
Built-in tag reference
Loads a template and renders it with the current context. This is a way of “including” other templates within a template. The template name can either be a variable or a hard-coded (quoted) string, in either single or double quotes.
Topic guide
Models
Fields
Django places some restrictions on model field names: A field name cannot be a Python reserved word, because that would result in a Python syntax error.
Topic guide
Making queries
To retrieve objects from your database, construct a QuerySet via a Manager on your model class. A QuerySet represents a collection of objects from your database. It can have zero, one or many filters.
Topic guide
Making queries
Retrieving objects
Use a subset of Python’s array-slicing syntax to limit your QuerySet to a certain number of results. This is the equivalent of SQL’s LIMIT and OFFSET clauses.
Topic guide
Making queries
Retrieving objects
Field lookups are how you specify the meat of an SQL WHERE clause. They’re specified as keyword arguments to the QuerySet methods filter(), exclude() and get(). Basic lookups keyword arguments take the form field__lookuptype=value. (That’s a double-underscore).
Topic guide
Making queries
Retrieving objectsLookups that span relationships
NOTE: The behavior of filter() for queries > that span multi-value relationships, as described above, is not implemented > equivalently for exclude(). Instead, > the conditions in a single exclude() > call will not necessarily refer to the same item.
Topic guide
Making queries
Retrieving objects
The field lookups that equate to LIKE SQL statements (iexact, contains, icontains, startswith, istartswith, endswith and iendswith) will automatically escape the two special characters used in LIKE statements – the percent sign and the underscore.
Topic guide
Performing raw SQL queries
The raw() manager method can be used to perform raw SQL queries that return model instances: Manager.raw(raw_query,params=(),translations=None) This method takes a raw SQL query, executes it, and returns a django.db.models.query.RawQuerySet instance.
Topic guide
Performing raw SQL queries
Performing raw queries
You can also execute queries containing fields that aren’t defined on the model.
Topic guide
Performing raw SQL queries
Sometimes even Manager.raw() isn’t quite enough: you might need to perform queries that don’t map cleanly to models, or directly execute UPDATE, INSERT, or DELETE queries.
Topic guide
Database transactions
Database-specific notes
While SQLite supports savepoints, a flaw in the design of the sqlite3 module makes them hardly usable. When autocommit is enabled, savepoints don’t make sense. When it’s disabled, sqlite3 commits implicitly before savepoint statements.
Topic guide
Managing files
File storage
You can use a callable as the storage parameter for FileField or ImageField. This allows you to modify the used storage at runtime, selecting different storages for different environments, for example.
Topic guide
Creating forms from models
ModelForm
As you might expect, the ForeignKey and ManyToManyField model field types are special cases: ForeignKey is represented by django.forms.ModelChoiceField, which is a ChoiceField whose choices are a model QuerySet.
Topic guide
Creating forms from models
ModelForm
Consider this set of models: With these models, the ModelForm subclasses above would be roughly equivalent to this (the only difference being the save() method, which we’ll discuss in a moment.):
Topic guide
Templates
Support for template engines
The django.template.loader module defines two functions to load templates. get_template(template_name,using=None)[source] : This function loads the template with the given name and returns a Template object. select_template(template_name_list,using=None)[source] : select_template() is just like get_template(), except it takes a list of template names.
Topic guide
Testing tools
Provided test case classes
classTransactionTestCase[source] TransactionTestCase inherits from SimpleTestCase to add some database-specific features: Resetting the database to a known state at the beginning of each test to ease testing and using the ORM. Database fixtures. Test skipping based on database backend features.
How-to
How to write custom lookups
The custom lookup above is great, but in some cases you may want to be able to chain lookups together. For example, let’s suppose we are building an application where we want to make use of the abs() operator.
How-to
How to write custom lookups
The AbsoluteValue example we discussed previously is a transformation which applies to the left-hand side of the lookup. There may be some cases where you want the transformation to be applied to both the left-hand side and the right-hand side.
How-to
How to create custom model fields
When planning your Field subclass, first give some thought to which existing Field class your new field is most similar to. Can you subclass an existing Django field and save yourself some work?