1 / 15

Django Forms

Forms in Django

Nirmala10
Download Presentation

Django Forms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Django Forms Prof. Nirmala Baloorkar Asst. Prof. Dept. of Computer Engineering

  2. Django's role in forms Handling forms is a complex business. Django handles three distinct parts of the work involved in forms: • preparing and restructuring data to make it ready for rendering • creating HTML forms for the data • receiving and processing submitted forms and data from the client It is possible to write code that does all of this manually, but Django can take care of it all for you. • Click Here

  3. Django forms act as "glue" • Generate the necessary HTML to send to the browser • Allow for consistent look and feel across all the forms in an application • Receive the POST data coming back from the browser • Validate the incoming POST data and produce HTML for an error screen if necessary • Move the data from the form into a model and then store it in the database automatically • The declaration syntax for a Form is very similar to that for declaring a Model, and shares the same field types (and some similar parameters).

  4. Creating Form • Creating a form in Django is completely similar to creating a model (django.forms.Form class), one needs to specify what fields (django.forms.Field subclasses) would exist in the form and of what type. • Click Here for form field

  5. Example - Creating Forms • Click Here for form field

  6. View & Url

  7. Rendering Form • Form actually rendered in the template? • We simply instantiate the Form class and pass it to the render function in a view, using the context, just like any other variable. • Simple Tag call {{form}} used to render the form. • A form comes with 3 in-built methods that can be used to render Django form fields. • {{ form.as_table }} will render them as table cells wrapped in <tr> tags • {{ form.as_p }} will render them wrapped in <p> tags • {{ form.as_ul }} will render them wrapped in <li> tags

  8. Rendering Form (cont…)

  9. Output

  10. Rendering Form (cont…)

  11. Validating Forms • A form is bound if it is called with some data to be used for validation, such as the POST data. • The is_valid method to check the form's validity, then the cleaned_data attribute on the form, which contains the values converted from strings to Python objects. • The cleaned_data attribute is only available after the form has been cleaned, which means the process of "cleaning up" the data and converting it from strings to Python objects. • The cleaning process runs during the is_valid call. • You will get AttributeError raised if you try to access cleaned_data before calling is_valid.

  12. Example

  13. Example

  14. Validation • Django provides numerous places where you can validate your data. • A bound form allows us to start using built-in validation-related tools: first, the is_valid method to check the form's validity, then to validate a single field is to override the method clean_<fieldname>() for the field you want to check.

  15. Validation

More Related