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
Provided test case classes
… callable raises a certain exception | | assertWarnsMessage() | That a callable triggers a certain warning | | assertFieldOutput() | Form field rendering and error output | | assertContains() | Presence or absence of HTML fragments | | assertTemplateUsed() | …
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.
Index
… opclasses (Index attribute) - (UniqueConstraint attribute) - open() (FieldFile method) - (File method) - (Storage method) - OpenLayersWidget (class in django.contrib.gis.forms.widgets) - OperationalError - OperationCategory (class in django.db.migrations.operations.base) - optimizemigration - django-admin command - optimizemigration command line option - --check …
How-to
How to use Django’s Content Security Policy
To use nonces in your CSP policy, beside the basic config, you need to: Include the NONCE placeholder value in the CSP settings. This only applies to script-src or style-src directives: Add the csp() context processor to your TEMPLATES setting.
How-to
How to use Django’s CSRF protection
Using CSRF protection with AJAX
… the JavaScript Cookie library to replace getCookie: NOTE: The CSRF token is also present in the DOM in a masked form, but only if > explicitly included using csrf_token in a template. The cookie > contains the canonical, unmasked token. …
Internals
Django Deprecation Timeline
The undocumented BaseForm._html_output() method will be removed. The ability to return a str, rather than a SafeString, when rendering an ErrorDict and ErrorList will be removed. See the Django 4.1 release notes for more details on these changes.
Internals
Django Deprecation Timeline
The following private APIs will be removed from django.db.models.options.Options (Model._meta): get_field_by_name() get_all_field_names() get_fields_with_model() get_concrete_fields_with_model() get_m2m_with_model() get_all_related_objects() get_all_related_objects_with_model() get_all_related_many_to_many_objects() get_all_related_m2m_objects_with_model() The error_message argument of django.forms.RegexField will be removed. The unordered_list filter will no longer support old style lists.
Internals
Django Deprecation Timeline
The model and form IPAddressField will be removed. A stub field will remain for compatibility with historical migrations. AppCommand.handle_app() will no longer be supported. RequestSite and get_current_site() will no longer be importable from django.contrib.sites.models.