Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Basically, a model encapsulates information about something (i.e., it <em>models</em> it), and is stored in the database. For example, we could model a person:</p> <pre><code>from django import models class Person(models.Model): name = models.CharField(max_length=100) age = models.PositiveIntegerField() height = models.FloatField() weight = models.FloatField() </code></pre> <p>Whenever a model instance is created and saved, Django stores it in the database for you to retrieve and use at a later date.</p> <p>On the other hand, forms correspond to HTML forms, i.e., a set of fields which are presented to the end user to fill some data in. A form can be completely independent of a model, for example a search form:</p> <pre><code>from django import forms class SearchForm(forms.Form): search_terms = forms.CharField(max_length=100) max_results = forms.IntegerField() </code></pre> <p>When submitted, Django takes care of validating the values the user entered and converting them to Python types (such as integers). All you then have to do is write the code which does something with these values.</p> <p>Of course, if you have created a model, you will often want to allow a user to create these models through a form. Instead of having to duplicate all the field names and create the form yourself, Django provides a shortcut for this, the <code>ModelForm</code>:</p> <pre><code>from django.forms import ModelForm class PersonForm(forms.ModelForm) class Meta: model = Person </code></pre> <p>As for further reading, I would start with the <a href="http://docs.djangoproject.com/en/dev/">Django documentation</a>, which includes a tutorial on creating and using models, and a fairly in-depth look at forms. There are also plenty of Django books and online tutorials to help you along.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload