1👍
You should create the form not in your template, but you have to create it in your view and pass it to the template.
Read Forms
Example form in forms.py:
from django import forms
class ExampleForm(forms.Form):
field = forms.CharField(label='Message', max_length=80)
Example view in views.py:
from django.shortcuts import render
from django.http import HttpResponse
from .forms import ExampleForm
def example_view(request):
if request.method == 'POST':
form = ExampleForm(request.POST)
if form.is_valid():
field1 = form.cleaned_data['field1']
# Do what you gotta do.
return HttpResponse("")
else:
form = ExampleForm()
return render(request, 'template.html', {'form': form})
Example template file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Your Example Form</title>
</head>
<body>
<form class="ExampleForm" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
</body>
</html>
Source:stackexchange.com