Last modified: Jan 11, 2023 By Alexander Williams
request post example in django
lets start by create a form.
First, create a forms.py file inside the directory of your blog application and make it look like this:
forms.py
#forms.py
from django import forms
class login_Form(forms.Form):
username = forms.CharField(max_length=25)
password = forms.CharField(widget=forms.PasswordInput)
views.py
#view.py
from .forms import *
def login(request):
form = login_Form()
# Handling forms
if request.method == "POST":
form = login_Form(request.POST)
if form.is_valid():
return HttpResponse('The form has been sent')
else:
form = login_Form()
return render(request, 'login.html', {'form':form})
urls.py
#url.py
path('login', login, name='form_login'),
login.html
#login.html
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="{% url 'form_login' %}" method="POST">
{% csrf_token %}
{{form}}
<input type="submit" name="submit" value="POST">
</form>
</body>
</html>
after submitting the form, we'll get this message
The form has been sent