O'Reilly logo

Learning Django Web Development by Ratan Kumar, Sanjeev Jaiswal

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Designing the tweet post form

Let's start by creating our first Django form. Create a new file in the tweets application folder and call it forms.py. Then, open the file in your code editor and enter the following code:

  from django import forms
  class TweetForm(forms.Form):
    text = forms.CharField(widget=forms.Textarea(attrs={'rows': 1, 'cols': 85}), max_length=160)
    country = forms.CharField(widget=forms.HiddenInput())

After examining the code, you will notice that the way in which we defined this class is similar to the way in which we defined the model classes. We derived the TweetForm class from forms.Form. All form classes need to inherit from this class. Next, we define the fields that this form contains:

 text = forms.CharField(widget=forms.Textarea(attrs={'rows': ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required