How do forms work




















Expand your skills. Get new features first. Was this information helpful? Yes No. Thank you! Any more feedback? The more you tell us the more we can help. Can you help us improve? Resolved my issue. Clear instructions. Easy to follow. No jargon. Rendering a form in a template involves nearly the same work as rendering any other kind of object, but there are some key differences. In the case of a model instance that contained no data, it would rarely if ever be useful to do anything with it in a template.

So when we handle a model instance in a view, we typically retrieve it from the database. When the form is submitted, the POST request which is sent to the server will contain the form data. This is a very simple form. In practice, a form might contain dozens or hundreds of fields, many of which might need to be pre-populated, and we might expect the user to work through the edit-submit cycle several times before concluding the operation.

We might require some validation to occur in the browser, even before the form is submitted; we might want to use much more complex fields, that allow the user to do things like pick dates from a calendar and so on. We already know what we want our HTML form to look like. Our starting point for it in Django is this:. This does two things. It also means that when Django receives the form back from the browser, it will validate the length of the data.

When this method is called, if all fields contain valid data, it will:. 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. To handle the form we need to instantiate it in the view for the URL where we want it to be published:. If we arrive at this view with a GET request, it will create an empty form instance and place it in the template context to be rendered.

This is what we can expect to happen the first time we visit the URL. This time the form is no longer empty unbound so the HTML form will be populated with the data previously submitted, where it can be edited and corrected as required. We can use this data to update the database or do other processing before sending an HTTP redirect to the browser telling it where to go next.

Django ships with an easy-to-use protection against Cross Site Request Forgeries. However, since CSRF protection is not directly tied to forms in templates, this tag is omitted from the following examples in this document. If you would like to disable this behavior, set the novalidate attribute on the form tag, or specify a different widget on the field, like TextInput. Once you understand the basics of the process described above, you should be prepared to understand other features of the forms system and ready to learn a bit more about the underlying machinery.

All form classes are created as subclasses of either django. Form or django. You can think of ModelForm as a subclass of Form. Form and ModelForm actually inherit common functionality from a private BaseForm class, but this implementation detail is rarely important. In fact if your form is going to be used to directly add or edit a Django model, a ModelForm can save you a great deal of time, effort, and code, because it will build a form, along with the appropriate fields and their attributes, from a Model class.

The distinction between Bound and unbound forms is important:. In a typical form submission, the response is a new page which the browser loads. Typically the new page replaces the current one, but this can be overridden with the target attribute.

There are a lot of form-related elements — different kinds of buttons, inputs, selectors for various types, feedback mechanisms… all sorts of things. In short: forms are complicated , maybe the most complicated aspect of HTML.

See our main forms index page for links to all of our form-related tutorials and documentation. Disclosure: Your support helps keep the site running!

We earn a referral fee for some of the services we recommend on this page. Learn more.



0コメント

  • 1000 / 1000