I have just started learning django. And when i started a small login system, i had to develop a form for it. I used a html page for it, and processed the data in views.py
and then put the response in another view. But i came across some file called forms.py
in the examples given. I don't understand how to use it, which is easy to use and better to use between the two. Can anyone help 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.
Django's form handling uses all of the same techniques that we learned about in previous tutorials (for displaying information about our models): the view gets a request, performs any actions required including reading data from the models, then generates and returns an HTML page (from a template, into which we pass a ...
Forms can resemble paper or database forms because web users fill out the forms using checkboxes, radio buttons, or text fields. For example, forms can be used to enter shipping or credit card data to order a product, or can be used to retrieve search results from a search engine.
Django ModelForm is a class that is used to directly convert a model into a Django form. If you're building a database-driven app, chances are you'll have forms that map closely to Django models. For example, a User Registration model and form would have the same quality and quantity of model fields and form fields.
forms.py
(like admin.py
) is just a convention that is used by django. It is considered best practice but not a strict requirement. There is nothing special or exotic about these files.
forms.py
is where the django documentation recommends you place all your forms code; to keep your code easily maintainable. Also, since its a convention mentioned in the documentation, it helps when you are collaborating with others because that is where others will expect to look for your code dealing with forms.
Think of it like index.html
. There is no rule that states that this file should be called index.html
, but its a convention that has been built over time.
You can have all your forms classes in your views.py
and nothing will break. Now if you are not using forms at all, and all still processing GET and POST data "manually", this you should stop immediately; especially if it is anything more than search query string.
The only file that is absolutely required is models.py
.
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