Account with this Email already exists. how to fix this?

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

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?