Forms let users send information to your website. Displaying forms in templates shows these input fields on web pages so users can fill them out.
Displaying forms in templates in Django
Start learning this pattern below
Jump into concepts and practice - no test required
{% csrf_token %}
{{ form.as_p }}{% csrf_token %} protects your form from security attacks.
{{ form.as_p }} renders the form fields wrapped in paragraph tags for simple layout.
{% csrf_token %}
{{ form.as_table }}{% csrf_token %}
{{ form.as_ul }}{% csrf_token %}
<label for="id_name">Name:</label>
{{ form.name }}This example creates a simple contact form with name, email, and message fields. The view sends the form to the template. The template displays the form fields inside paragraphs with a submit button.
from django import forms from django.shortcuts import render class ContactForm(forms.Form): name = forms.CharField(max_length=100) email = forms.EmailField() message = forms.CharField(widget=forms.Textarea) def contact_view(request): form = ContactForm() return render(request, 'contact.html', {'form': form}) # contact.html template content: # <form method="post"> # {% csrf_token %} # {{ form.as_p }} # <button type="submit">Send</button> # </form>
Always include {% csrf_token %} inside your form for security.
You can customize form field rendering by manually writing HTML and using {{ form.field_name }}.
Use form.as_p, form.as_table, or form.as_ul for quick layouts.
Displaying forms in templates shows input fields for users to fill.
Use {% csrf_token %} and {{ form.as_p }} to render forms safely and simply.
You can customize form display by rendering fields individually.
Practice
{% csrf_token %} in a Django form template?Solution
Step 1: Understand CSRF protection in Django
Django uses{% csrf_token %}to add a hidden token to forms that helps prevent malicious attacks from other sites.Step 2: Identify the role of the token in form security
This token is checked on form submission to ensure the request is from the original site, protecting against CSRF attacks.Final Answer:
To protect the form from Cross-Site Request Forgery attacks -> Option AQuick Check:
CSRF token = security protection [OK]
- Thinking it styles the form
- Confusing it with form submission action
- Assuming it shows errors
Solution
Step 1: Recall Django form rendering methods
Django forms have built-in methods likeas_p,as_table, andas_ulto render fields in different HTML formats.Step 2: Identify the correct syntax for paragraph rendering
The correct syntax to render form fields wrapped in paragraphs is{{ form.as_p }}. The other options are either wrong methods or incorrect template syntax.Final Answer:
{{ form.as_p }} -> Option AQuick Check:
Render form as paragraphs = {{ form.as_p }} [OK]
- Using template tags {% %} instead of {{ }} for form rendering
- Calling non-existent render() method
- Confusing as_table with as_p
<form method="post">
{% csrf_token %}
{{ form.username }}
{{ form.password }}
<button type="submit">Login</button>
</form>What will be displayed for
{{ form.username }} and {{ form.password }}?Solution
Step 1: Understand rendering individual form fields
Rendering{{ form.fieldname }}outputs the HTML input element only for that field, without the label.Step 2: Confirm output for username and password fields
Each field renders as an input box without its label, so both username and password fields will appear as input fields.Final Answer:
Input fields for username and password -> Option BQuick Check:
Individual field rendering = input fields [OK]
- Thinking {{ form.field }} shows only label text
- Assuming as_p is required for any output
- Confusing empty output with errors
<form method="post">
{{ form.as_p }}
</form>But when submitting, you get a CSRF verification failed error. What is missing?
Solution
Step 1: Identify cause of CSRF verification failure
Django requires a CSRF token in POST forms to verify requests. Missing{% csrf_token %}causes this error.Step 2: Fix the template by adding CSRF token
Insert{% csrf_token %}inside the form tags to include the hidden token for security.Final Answer:
You forgot to include {% csrf_token %} inside the form -> Option DQuick Check:
CSRF error = missing {% csrf_token %} [OK]
- Changing method to GET instead of adding token
- Trying to validate form in template
- Switching form rendering method without token
Solution
Step 1: Understand how to render labels and fields separately
Django form fields have alabel_tagmethod that outputs the label HTML correctly linked to the input.Step 2: Compare options for correct label rendering
<form method="post"> {% csrf_token %} {{ form.name.label_tag }} {{ form.name }} {{ form.email.label_tag }} {{ form.email }} <button type="submit">Send</button> </form>uses{{ form.name.label_tag }}and{{ form.name }}which is the proper way to show label and input separately.<form method="post"> {% csrf_token %} <label for="name">Name:</label> {{ form.name }} <label for="email">Email:</label> {{ form.email }} <button type="submit">Send</button> </form>uses manual labels which may not link properly.<form method="post"> {{ form.name.label }} {{ form.name }} {{ form.email.label }} {{ form.email }} <button type="submit">Send</button> </form>useslabelwhich is just text, not HTML label tag.Final Answer:
{{ form.field.label_tag }} {{ form.field }} -> Option CQuick Check:
Use label_tag for proper label HTML [OK]
- Using plain text labels without label_tag
- Forgetting csrf_token in form
- Using label instead of label_tag for labels
