djangodocs.org

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

Prefixes for forms

Reference The Forms API

Form.prefix You can put several Django forms inside one <form> tag. To give each Form its own namespace, use the prefix keyword argument: The prefix can also be specified on the form class:

4.2 5.0 5.1 5.2 6.0

formset_factory

Reference Formset Functions

formset_factory(form,formset=BaseFormSet,extra=1,can_order=False,can_delete=False,max_num=None,validate_max=False,min_num=None,validate_min=False,absolute_max=None,can_delete_extra=True,renderer=None)[source] : Returns a FormSet class for the given form class.

4.2 5.0 5.1 5.2 6.0

The view

Topic guide Working with forms Building a formBuilding a form in Django

Form data sent back to a Django website is processed by a view, generally the same view which published the form. This allows us to reuse some of the same logic.

4.2 5.0 5.1 5.2 6.0

Widgets

Topic guide Working with forms More about Django Form classesMore on fields

Each form field has a corresponding Widget class, which in turn corresponds to an HTML form widget such as <input type="text">. In most cases, the field will have a sensible default widget.

4.2 5.0 5.1 5.2 6.0

Field types

Topic guide Creating forms from models ModelForm

The generated Form class will have a form field for every model field specified, in the order specified in the fields attribute. Each model field has a corresponding default form field.

5.2 6.0

Field types

Topic guide Creating forms from models ModelForm

The generated Form class will have a form field for every model field specified, in the order specified in the fields attribute. Each model field has a corresponding default form field.

4.2 5.0 5.1

Assertions

Topic guide Testing tools Test cases features

SimpleTestCase.assertFormSetError(formset,form_index,field,errors,msg_prefix='')[source] : Asserts that the formset raises the provided list of errors when rendered. SimpleTestCase.assertContains(response,text,count=None,status_code=200,msg_prefix='',html=False)[source] : Asserts that a response produced the given status_code and that text appears in its content.

4.2 5.0 5.1 5.2 6.0

ModelFormMixin

Reference Editing mixins

classdjango.views.generic.edit.ModelFormMixin : A form mixin that provides facilities for working with a ModelForm, rather than a standalone form.

dev

Other methods

Reference The Django admin site

ModelAdmin.add_view(request,form_url='',extra_context=None)[source] : Django view for the model instance addition page. See note below. ModelAdmin.change_view(request,object_id,form_url='',extra_context=None)[source] : Django view for the model instance editing page. See note below. ModelAdmin.changelist_view(request,extra_context=None)[source] : Django view for the model instances change list/actions page. See note below.

dev

GeoDjango Forms API

Reference

GeoDjango provides some specialized form fields and widgets in order to visually display and edit geolocalized data on a map. By default, they use OpenLayers-powered maps, with a base WMS layer provided by NASA.

dev

Output styles

Reference The Forms API

The recommended approach for changing form output style is to set a custom form template either site-wide, per-form, or per-instance. See Reusable form templates for examples.

dev

Using mixins

Topic guide Introduction to class-based views

Mixins are a form of multiple inheritance where behaviors and attributes of multiple parent classes can be combined. For example, in the generic class-based views there is a mixin called TemplateResponseMixin whose primary purpose is to define the method render_to_response().

dev

More on fields

Topic guide Working with forms

Consider a more useful form than our minimal example above, which we could use to implement “contact me” functionality on a personal website: forms.py Our earlier form used a single field, your_name, a CharField.

dev

Basic file uploads

Topic guide File uploads

Consider a form containing a FileField: forms.py A view handling this form will receive the file data in request.FILES, which is a dictionary containing a key for each FileField (or ImageField, or other FileField subclass) in the form.

dev

ModelFormMixin

Reference Editing mixins

classdjango.views.generic.edit.ModelFormMixin : A form mixin that provides facilities for working with a ModelForm, rather than a standalone form.

4.2 5.0 5.1 5.2 6.0

Other methods

Reference The Django admin site ModelAdmin objectsModelAdmin methods

ModelAdmin.add_view(request,form_url='',extra_context=None)[source] : Django view for the model instance addition page. See note below. ModelAdmin.change_view(request,object_id,form_url='',extra_context=None)[source] : Django view for the model instance editing page. See note below. ModelAdmin.changelist_view(request,extra_context=None)[source] : Django view for the model instances change list/actions page. See note below.

4.2 5.0 5.1 5.2 6.0

GeoDjango Forms API

Reference

GeoDjango provides some specialized form fields and widgets in order to visually display and edit geolocalized data on a map. By default, they use OpenLayers-powered maps, with a base WMS layer provided by NASA.

4.2 5.0 5.1 5.2 6.0