Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do you define the django forms?

Tags:

django-forms

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)
like image 906
starcorn Avatar asked Jan 14 '12 16:01

starcorn


People also ask

Where should I create forms py in Django?

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.

How do you customize a form in Django?

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.

What is the use of forms py in Django?

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.


1 Answers

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

like image 157
jdi Avatar answered Oct 03 '22 13:10

jdi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!