I am starting a new project again in django and so far i have never used django forms. Now i am wondering why i should use it and what it would save me. I still feel, by using django forms, i am not free in terms of html styling with frontend frameworks like bootstrap.
why should I use django forms?
can you guys please guide me a bit?
Django provides a Form class which is used to create HTML forms. It describes a form and how it works and appears. It is similar to the ModelForm class that creates a form by using the Model, but it does not require the Model.
With Django forms, the standard forms HTML is auto-generated for you and you have to write code for only where you want to customize the form. Django forms also handle form validation for you, and you can override with self-written code only when you want some special case validation. Save this answer.
If you're creating a form in which the data goes into a database table, Django's form is easier as well. The data is easily transferrable from form to database table with Django forms. If you creating any type of complex form that creates complex styling, it's easier to go with plain HTML forms.
Django-crispy-forms is an application that helps to manage Django forms. It allows adjusting forms' properties (such as method, send button or CSS classes) on the backend without having to re-write them in the template.
i am not free in terms of html styling with frontend frameworks like bootstrap.
There is a very easy solution to this: use django-crispy-forms
and regain all control of what your form looks like frontend. There is good documentation which includes parts on how to make your forms use bootstrap.
i am wondering why i should use it and what it would save me.
The question really is "do you have any good reason not to use django.forms?" Because really, if you are not using django.forms you throw away a lot of django's built-in security measures. Even within django itself (for instance the admin site) forms are used heavily. Thus I doubt your previous project really did not use django forms.
If you are building an API you might want to look at how tastypie or django rest framework go about validation data.
From Django docs,
using the form library takes care of a number of common form-related tasks. Using it, you can:
Display an HTML form with automatically generated form widgets.
Check submitted data against a set of validation rules.
Redisplay a form in the case of validation errors.
Convert submitted form data to the relevant Python data types.
For more info, please read that documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With