Issue
This Content is from Stack Overflow. Question asked by olooliver
I tried to create a login form. But when I try to login django gives the error that Account with this Email already exists. i don`t now how to fix this.Help me plz
Here is my code, please tell me where I’m going wrong:
forms.py
class AccountAuthenticationForm(forms.ModelForm):
password = forms.CharField(label='Password', widget=forms.PasswordInput)
class Meta:
model= Account
fields = ('email', 'password')
def clen(self):
email = self.cleaned_data['email']
password = self.cleaned_data['password']
if not authenticate(email=email, password=password):
raise forms.ValidationError("Invalid login")
views.py
def login_view(request):
context = {}
user = request.user
if user.is_authenticated:
return redirect("home")
if request.POST:
form = AccountAuthenticationForm(request.POST)
if form.is_valid():
email=request.POST['email']
password = request.POST['password']
user =authenticate(email=email, password=password)
if user:
login(request,user)
return redirect("home")
else:
form = AccountAuthenticationForm()
context['login_form'] = form
return render(request, 'account/login.html', context)
login.html
{% extends 'base.html' %}
{% block content %}
<h2>Login</h2>
<form method="post">{% csrf_token %}
{% for field in login_form %}
<p>
{{field.label_tag}}
{{field}}
{% if field.help_text %}
<small style="color:grey">{field.help_text}</small>
{% endif %}
{% for error in field.errors %}
<p style="color:red">{{error}}</p>
{% endfor %}
{% if login_form.non_field_errors %}
<div style="color:red";>
<p>{{login_form.non_field_errors}}</p>
</div>
{% endif %}
</p>
{% endfor %}
<button type="submit">Log in</button>
</form>
{% endblock content %}
I’m just getting started with Django. I will be glad if you point out my mistake
Solution
solved
i just added email field to the class AccountAuthentcationForm
in forms.py
and changed from forms.ModelForm
to forms.Form
This Question was asked in StackOverflow by olooliver and Answered by olooliver It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.