0
0
Djangoframework~10 mins

Why Django forms matter - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import Django's form class.

Django
from django import [1]
Drag options to blanks, or click blank then click option'
Aviews
Bforms
Curls
Dmodels
Attempts:
3 left
💡 Hint
Common Mistakes
Importing models instead of forms
Trying to import views or urls here
2fill in blank
medium

Complete the code to define a simple Django form with a single text field.

Django
class NameForm([1].Form):
    your_name = [1].CharField(label='Your name', max_length=100)
Drag options to blanks, or click blank then click option'
Aviews
Bmodels
Cforms
Dfields
Attempts:
3 left
💡 Hint
Common Mistakes
Using models instead of forms
Forgetting to use the forms module prefix
3fill in blank
hard

Fix the error in the form validation code by completing the blank.

Django
if request.method == 'POST':
    form = NameForm(request.[1])
    if form.is_valid():
        # process data
Drag options to blanks, or click blank then click option'
APOST
BDATA
CFILES
DGET
Attempts:
3 left
💡 Hint
Common Mistakes
Using request.GET instead of request.POST
Using request.FILES without file inputs
4fill in blank
hard

Fill both blanks to create a form that includes an email field and a submit button in the template.

Django
<form method='post'>
  {% csrf_token %}
  {{ form.[1] }}
  <button type='[2]'>Submit</button>
</form>
Drag options to blanks, or click blank then click option'
Aas_p
Btext
Csubmit
Das_table
Attempts:
3 left
💡 Hint
Common Mistakes
Using as_table or text instead of as_p
Using button type other than 'submit'
5fill in blank
hard

Fill all three blanks to create a form class with a required email field, a clean method to validate it, and raise an error if invalid.

Django
class EmailForm(forms.Form):
    email = forms.[1](required=True)

    def clean_email(self):
        email = self.cleaned_data.get('[2]')
        if not email.endswith('[3]'):
            raise forms.ValidationError('Email must end with example.com')
        return email
Drag options to blanks, or click blank then click option'
AEmailField
Bemail
Cexample.com
DCharField
Attempts:
3 left
💡 Hint
Common Mistakes
Using CharField instead of EmailField
Checking wrong cleaned_data key
Incorrect domain string in validation