Topic guide
Working with forms
Forms in Django
… the database, for example) pass it to the template context expand it to HTML markup using template variables Rendering a form in a template involves nearly the same work as rendering any other kind of object, but there are some …
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
Creating forms from models
ModelForm
You can create forms from a given model using the standalone function modelform_factory(), instead of using a class definition.
Topic guide
Creating forms from models
classmodels.BaseModelFormSet Like regular formsets, Django provides a couple of enhanced formset classes to make working with Django models more convenient. Let’s reuse the Author model from above: Using fields restricts the formset to use only the given fields.
Topic guide
Creating forms from models
Model formsets
As with a ModelForm, you can save the data as a model object. This is done with the formset’s save() method: The save() method returns the instances that have been saved to the database.
Topic guide
Creating forms from models
Model formsets
There are three ways to render a formset in a Django template.
Topic guide
File Uploads
Basic file uploads
If you’re saving a file on a Model with a FileField, using a ModelForm makes this process much easier.
Topic guide
How to use sessions
As a convenience, Django provides a way to test whether the user’s browser accepts cookies. Call the set_test_cookie() method of request.session in a view, and call test_cookie_worked() in a subsequent view – not in the same view call.
Topic guide
Format localization
When formatting is enabled, Django can use localized formats when parsing dates, times and numbers in forms. That means it tries different formats for different locales when guessing the format used by the user when inputting data on forms.
Topic guide
Internationalization and localization
localization Writing the translations and local formats. Usually done by translators. More details can be found in the W3C Web Internationalization FAQ, the Wikipedia article or the GNU gettext documentation. WARNING: Translation is controlled by the USE_I18N setting.
Topic guide
Time zones
Migration guide
Since Django now gives you aware datetimes, you’ll get exceptions wherever you compare a datetime that comes from a model or a form with a naive datetime that you’ve created in your code.
Topic guide
Time zones
FAQ
… your code is comparing these two things: a datetime provided by Django – for instance, a value read from a form or a model field. Since you enabled time zone support, it’s aware. a datetime generated by your code, which …
Topic guide
Security in Django
This ensures that a malicious user cannot “replay” a form POST to your website and have another logged-in user unwittingly submit that form. The malicious user would have to know the secret, which is user specific (using a cookie).
Topic guide
Django’s serialization framework provides a mechanism for “translating” Django models into other formats.
Topic guide
Ensuring data stored in hidden form fields has not been tampered with. Generating one-time secret URLs for allowing temporary access to a protected resource, for example a downloadable file that a user has paid for.
Topic guide
Testing tools
Test cases features
utils.isolate_apps(*app_labels,attr_name=None,kwarg_name=None) : Registers the models defined within a wrapped context into their own isolated apps registry.
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.
6.0
dev
Reference
django.contrib.auth
Authentication backends
classAllowAllUsersModelBackend[source] : Same as ModelBackend except that it doesn’t reject inactive users because user_can_authenticate() always returns True. classRemoteUserBackend[source] : Use this backend to take advantage of external-to-Django-handled authentication. It authenticates using usernames passed in request.META['REMOTE_USER'].
Topic guide
Making queries
Querying JSONField
… based on a given dictionary key, use that key as the lookup name: Multiple keys can be chained together to form a path lookup: If the key is an integer, it will be interpreted as an index transform in an …
6.0
Internals
Django Deprecation Timeline
They will be removed. The form wizard has been refactored to use class-based views with pluggable backends in 1.4. The previous implementation will be removed. Legacy ways of calling cache_page() will be removed.
5.2
6.0
dev