Probably some simple question, but does it say in Django's homepage where you should define the forms? Does the code goes into the model.py? But that file should only consist of Django's database models right? So I wonder where should the forms go into?
Forms that I mean is this one you import
from django import forms
and then create for example a form like this (code is taken from Django's homepage)
class ContactForm(forms.Form):
subject = forms.CharField(max_length=100)
message = forms.CharField()
sender = forms.EmailField()
cc_myself = forms.BooleanField(required=False)
Form data is stored in an application's forms.py file, inside the application directory. Create and open the file locallibrary/catalog/forms.py. To create a Form , we import the forms library, derive from the Form class, and declare the form's fields.
So, we have to just design the HTML with css, and use it in Django by just replacing the input tag with render_field and form. field_name with {% %} at the end and design it with our choice.
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.
It doesn't really matter where you store your form classes. Django isn't specifically looking for them the way it does for models in your models.py
I usually store them in a forms.py module. Then you import from your forms module in your views.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