What is the difference between forms.Form and forms.ModelForm?

3

Hello, dear.

With the help of a tutorial, I was able to create a template with form and use the forms.ModelForm to save in the database, however, I saw in the Django documentation that there is also the forms.Form. What's the difference?

Django1.9 framework Python3.4

    
asked by anonymous 03.01.2016 / 21:38

2 answers

3

The difference is simple, ModelForm serves to create the form of a Model.

# Formulário que salva artigos no seu banco de dados.
class ArticleForm(ModelForm):
    class Meta:
        model = Artigo
        fields = ['pub_date', 'headline', 'content', 'reporter']

Form is a common form that is unrelated to your database (model ).

# formulário de contato para enviar via e-mail
class ContatoForm(forms.Form):
    subject = forms.CharField(max_length=100)
    message = forms.CharField(widget=forms.Textarea)
    
04.01.2016 / 14:06
0

Exactly as stated above ModelForm connects your Model to the Database, Form already connects the Model with the Interface, Views and Templates.

    
02.02.2016 / 18:35