djangodocs.org

1305 sections across all versions Narrow to Django 6.0 (current) →

Topic guide Formsets

The absolute_max parameter to formset_factory() allows limiting the number of forms that can be instantiated when supplying POST data. This protects against memory exhaustion attacks using forged POST requests: When absolute_max is None, it defaults to max_num + 1000.

dev

error_messages

Topic guide Formsets

The error_messages argument lets you override the default messages that the formset will raise. Pass in a dictionary with keys matching the error messages you want to override. Error message keys include 'too_few_forms', 'too_many_forms', and 'missing_management_form'.

dev

can_order

Topic guide Formsets

BaseFormSet.can_order Default: False Lets you create a formset with the ability to order: This adds an additional field to each form. This new field is named ORDER and is an forms.IntegerField.

dev

can_delete

Topic guide Formsets

BaseFormSet.can_delete Default: False Lets you create a formset with the ability to select forms for deletion: Similar to can_order this adds a new field to each form named DELETE and is a forms.BooleanField.

dev

Topic guide Working with forms

However, it can also be directly accessed from the field attribute on the form. For example: Complete <label> elements can also be generated using the label_tag(). For example:

dev

ModelForm

Topic guide Creating forms from models

classModelForm[source] 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.

dev

A full example

Topic guide Creating forms from models

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.):

dev

Model formsets

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.

dev

Setting test cookies

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.

dev

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.

dev

Definitions

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.

dev

Code

Topic guide Time zones

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.

dev

Troubleshooting

Topic guide Time zones

… 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 …

dev

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).

dev