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


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:

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")

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():
            password = request.POST['password']
            user =authenticate(email=email, password=password)

            if user:
                return redirect("home")
        form = AccountAuthenticationForm()

    context['login_form'] = form
    return render(request, 'account/login.html', context)


{% extends 'base.html' %}

{% block content %}

<form method="post">{% csrf_token %}
    {% for field in login_form %}

            {% 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";>

            {% endif %}
    {% endfor %}
    <button type="submit">Log in</button>

{% endblock content %}

I’m just getting started with Django. I will be glad if you point out my mistake


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?