Topic guide
Creating forms from models
classModelForm If you’re building a database-driven app, chances are you’ll have forms that map closely to Django models. For instance, you might have a BlogComment model, and you want to create a form that lets people submit comments.
Topic guide
Creating forms from models
ModelForm
Calling save_m2m() is only required if you use save(commit=False). When you use a save() on a form, all data – including many-to-many data – is saved without the need for any additional method calls.
Topic guide
Creating forms from models
Model formsets
As with regular forms, it’s possible to specify initial data for forms by specifying an initial parameter when instantiating the form.
Topic guide
Creating forms from models
Model formsets
Using the localized_fields parameter, you can enable localization for fields in the form. If localized_fields is set to the special value '__all__', all fields will be localized.
Topic guide
Translation
Internationalization: in JavaScript codeUsing the JavaScript translation catalog
… function works in a similar way to the pluralize template filter, determining if a given count should use a plural form of a word or not: In the simplest case, if no custom pluralization is needed, this returns false for …
Topic guide
Translation
Localization: how to create language files
After you create your message file – and each time you make changes to it – you’ll need to compile it into a more efficient form, for use by gettext. Do this with the django-admin compilemessages utility.
Topic guide
Security in Django
Clickjacking is a type of attack where a malicious site wraps another site in a frame. This attack can result in an unsuspecting user being tricked into performing unintended actions on the target site.
Topic guide
Serializing Django objects
Serialization formatsJSON
A subclass of JSONEncoder, it handles these additional types: datetime A string of the form YYYY-MM-DDTHH:mm:ss.sssZ or YYYY-MM-DDTHH:mm:ss.sss+HH:MM as defined in ECMA-262. date A string of the form YYYY-MM-DD as defined in ECMA-262.
Topic guide
Testing tools
Provided test case classes
Testing form field rendering and error treatment. Testing HTML responses for the presence/lack of a given fragment. Verifying that a template has/hasn't been used to generate a given response content. Verifying that two URLs are equal.
Index
… django.conf.urls) - has_add_permission() (InlineModelAdmin method) - (ModelAdmin method) - has_change_permission() (InlineModelAdmin method) - (ModelAdmin method) - has_changed() (Field method) - (Form method) - has_delete_permission() (InlineModelAdmin method) - (ModelAdmin method) - has_error() (Form method) - has_header() (HttpResponse method) - has_module_permission() (ModelAdmin …
Index
… (BoundField attribute) - (Field attribute) - (LabelCommand attribute) - (Options attribute) - label_lower (Options attribute) - label_suffix (Field attribute) - (Form attribute) - label_tag() (BoundField method) - LabelCommand (class in django.core.management) - Lag (class in django.db.models.functions) - language - template …
How-to
… can use csrf_protect() on particular views you want to protect (see below). 2. In any template that uses a POST form, use the csrf_token tag inside the <form> element if the form is for an internal URL, e.g.: This should …
How-to
How to use Django’s CSRF protection
It must be used both on views that insert the CSRF token in the output, and on those that accept the POST form data. (These are often the same view function, but not always).
How-to
How to use Django’s CSRF protection
By default, a ‘403 Forbidden’ response is sent to the user if an incoming request fails the checks performed by CsrfViewMiddleware.
How-to
How to create custom model fields
Writing a field subclass
Writing a custom field can be a tricky process, particularly if you’re doing complex conversions between your Python types and your database and serialization formats.
How-to
How to create custom template tags and filters
Writing custom template tags
When you call render() on a compiled template object, the template calls render() on each Node in its node list, with the given context. The results are all concatenated together to form the output of the template.
How-to
How to provide initial data for models
Provide data with fixtures
Django also looks for fixtures in the list of directories provided in the FIXTURE_DIRS setting. To completely prevent default search form happening, use an absolute path to specify the location of your fixture file, e.g. loaddata /path/to/sample.
How-to
How to configure and use logging
… with the records they receive filters, to provide additional control over the transfer of records, and even modify records in-place formatters, to convert LogRecord objects to a string or other form for consumption by human beings or another system There …
Internals
Triaging tickets
Other triage attributes
Keyword search finds the keyword string anywhere in the keywords. For example, clicking on a ticket with the keyword “form” will yield similar tickets tagged with keywords containing strings such as “formset”, “modelformset”, and “ManagementForm”.
Internals
Django Deprecation Timeline
The undocumented function django.contrib.formtools.utils.security_hash will be removed, instead use django.contrib.formtools.utils.form_hmac The function-based generic view modules will be removed in favor of their class-based equivalents, outlined here. The django.core.servers.basehttp.AdminMediaHandler will be removed. In its place use django.contrib.staticfiles.handlers.StaticFilesHandler.