Forms

Detailed form API reference. For introductory material, see the Working with forms topic guide. The Forms APIBound and unbound forms Using forms to validate data Dynamic initial values Checking which form data has changed Accessing the fields from the form Accessing “clean” data Outputting forms as HTML More granular output Customizing BoundField Binding uploaded files to a form Subclassing forms Prefixes for forms Form fieldsCore field arguments Checking if the field data has changed Built

Form handling with class-based views

Form processing generally has 3 paths: Initial GET (blank or prepopulated form) POST with invalid data (typically redisplay form with errors) POST with valid data (process the data and typically redirect) Implementing this yourself often results in a lot of repeated boilerplate code (see Using a form in a view). To help avoid this, Django provides a collection of generic class-based views for form processing. Basic forms Given a simple contact form: from django import forms class ContactForm

Format localization

Overview Django’s formatting system is capable of displaying dates, times and numbers in templates using the format specified for the current locale. It also handles localized input in forms. When it’s enabled, two users accessing the same content may see dates, times and numbers formatted in different ways, depending on the formats for their current locale. The formatting system is disabled by default. To enable it, it’s necessary to set USE_L10N = True in your settings file. Note The default

flatpages.models.FlatPage

class FlatPage Flatpages are represented by a standard Django model, which lives in django/contrib/flatpages/models.py. You can access flatpage objects via the Django database API. Check for duplicate flatpage URLs. If you add or modify flatpages via your own code, you will likely want to check for duplicate flatpage URLs within the same site. The flatpage form used in the admin performs this validation check, and can be imported from django.contrib.flatpages.forms.FlatpageForm and used in

flatpages.sitemaps.FlatPageSitemap

class FlatPageSitemap [source] The sitemaps.FlatPageSitemap class looks at all publicly visible flatpages defined for the current SITE_ID (see the sites documentation) and creates an entry in the sitemap. These entries include only the location attribute – not lastmod, changefreq or priority.

Form and field validation

Form validation happens when the data is cleaned. If you want to customize this process, there are various places to make changes, each one serving a different purpose. Three types of cleaning methods are run during form processing. These are normally executed when you call the is_valid() method on a form. There are other things that can also trigger cleaning and validation (accessing the errors attribute or calling full_clean() directly), but normally they won’t be needed. In general, any clea

Form Assets (the Media class)

Rendering an attractive and easy-to-use Web form requires more than just HTML - it also requires CSS stylesheets, and if you want to use fancy “Web2.0” widgets, you may also need to include some JavaScript on each page. The exact combination of CSS and JavaScript that is required for any given page will depend upon the widgets that are in use on that page. This is where asset definitions come in. Django allows you to associate different files – like stylesheets and scripts – with the forms and

flatpages.middleware.FlatpageFallbackMiddleware

class FlatpageFallbackMiddleware Each time any Django application raises a 404 error, this middleware checks the flatpages database for the requested URL as a last resort. Specifically, it checks for a flatpage with the given URL with a site ID that corresponds to the SITE_ID setting. If it finds a match, it follows this algorithm: If the flatpage has a custom template, it loads that template. Otherwise, it loads the template flatpages/default.html. It passes that template a single context v

File Uploads

When Django handles a file upload, the file data ends up placed in request.FILES (for more on the request object see the documentation for request and response objects). This document explains how files are stored on disk and in memory, and how to customize the default behavior. Warning There are security risks if you are accepting uploaded content from untrusted users! See the security guide’s topic on User-uploaded content for mitigation details. Basic file uploads Consider a simple form co

File handling

The File objectThe File class The ContentFile class The ImageFile class Additional methods on files attached to objects File storage APIGetting the current storage class The FileSystemStorage class The Storage class Uploaded Files and Upload HandlersUploaded files Built-in upload handlers Writing custom upload handlers