I have a problem in my form. When I print the error it says "The field is required". can anyone tell me what I am missing.
here is my code
forms.py:
from django import forms
class LoginAuth(forms.Form):
Email= forms.EmailField()
Password= forms.CharField(max_length=300)
views.py:
from django.shortcuts import render
from .forms import LoginAuth
def login(request):
if request.method=="POST":
form = LoginAuth(request.POST)
if form.is_valid():
Email= form.cleaned_data['email']
Password= form.cleaned_data['pass']
request.session['user']= Email
request.session.set_expiry(2)
print("The form is validated")
else:
print(form.errors)
return render(request, "login.html")
login.html:
{% extends 'base.html' %}
{% block content %}
<form method="post">
{% csrf_token %}
<input type="email" name="email" placeholder="Enter Email"> <br>
<input type="password" name="pass" placeholder="Enter Password"> <br> <br>
<input type="submit">
</form>
{% endblock %}
The names are
EmailandPassowrdin your form, notemailandpass. You might want to rename these in the Django form to:then in the form use:
and in the view then use
passwordinstead ofpass: naming a variablepassis not possible in Python, or at least not without some meta-programming: