[SOLVED] 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


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.

people found this article helpful. What about you?