2

I wrote e-mail address and password which is already registed in login form but always login is rejected. I wrote in html like

<div class="container">
            <form class="form-inline" action="{% url 'login' %}" method="post" role="form">
                {% csrf_token %}
            <div class="form-group">
                <label class="email_form">Email</label>
                <input for="id_email" name="email" type="text" value="" placeholder="Email" class="form-control"/>
            </div>
            <div class="form-group">
                <label class="password_form">Password</label>
                <input id="id_password" name="password" type="password" value="" minlength="8" maxlength="12" placeholder="Password" class="form-control"/>
            </div>

            <button type="submit" class="btn btn-primary btn-lg">Login</button>
            <input name="next" type="hidden" value="{{ next }}"/>
            </form>

            </div>

in forms.py

class RegisterForm(UserCreationForm):
    class Meta:
        model = User
        fields = ('username', 'email',)
    def __init__(self, *args, **kwargs):
        super(RegisterForm, self).__init__(*args, **kwargs)
        self.fields['username'].widget.attrs['class'] = 'form-control'
        self.fields['email'].widget.attrs['class'] = 'form-control'
        self.fields['password1'].widget.attrs['class'] = 'form-control'
        self.fields['password2'].widget.attrs['class'] = 'form-control'

class LoginForm(AuthenticationForm):
    def __init__(self, *args, **kwargs):
        __init__(*args, **kwargs)
        self.fields['email'].widget.attrs['class'] = 'form-control'
        self.fields['password'].widget.attrs['classF'] = 'form-control'

I already confirmate whether email&password is right or not in admin site, but I am surely right to typed email&password in login's form.So I really cannot understand why I cannot log in.What is wrong in my code?How can I fix this?

user8681386
  • 111
  • 1
  • 8
  • By default, django's ModelBackend uses the `username` for authentication, not the `email`. – user2390182 Oct 02 '17 at 07:36
  • @schwobaseggl Oh really?So can't I login email&password by using Django? – user8681386 Oct 02 '17 at 07:40
  • Of course you can :) Set `USERNAME_FIELD = 'email'` in your user model (if you have a custom one) or write a custom auth backend which is fairly easy. Read the [docs on authentication](https://docs.djangoproject.com/en/1.11/topics/auth/customizing/). – user2390182 Oct 02 '17 at 07:51
  • A custom model might be appropriate since the email field is NOT unique in the default user model. – user2390182 Oct 02 '17 at 07:53
  • Possible duplicate of [Django - Login with Email](https://stackoverflow.com/questions/37332190/django-login-with-email) – user2390182 Oct 02 '17 at 08:14

0 Answers0