L
L
lukepker2019-08-03 20:48:38
Django
lukepker, 2019-08-03 20:48:38

How to fix the login page in django?

Hello!
There is a login page:

<form class="form-signin" method="post">
  {% csrf_token %}
  <input type="hidden" name="next" value="{{ next }}">
  <img class="mb-4" src="{% static "logo.svg" %}" alt="" width="72" height="72">


  <h1 class="h3 mb-3 font-weight-normal">Вход</h1>
  <label for="inputEmail" class="sr-only">Email</label>
  <input type="email" id="inputEmail" class="form-control" placeholder="Email" name="username"
         value="" required="" autofocus="" data-cip-id="inputEmail">
  <label for="inputPassword" class="sr-only">Password</label>
  <input type="password" id="inputPassword" class="form-control" placeholder="Пароль"
         name="password" required="" data-cip-id="inputPassword">

  <button class="btn btn-lg btn-primary btn-block" type="submit">Войти</button>
  <p class="mt-5 mb-3 text-muted">© Просто магазин 2018</p>
</form>

this is how it is written in urls
path('login/', views.LoginView.as_view(), name='login')

the settings say LOGIN_REDIRECT_URL = '/' for redirecting to the main page
So here's the problem:
if you create a user and then try to log in, then when you press the button, the page simply refreshes, the login does not occur,
I guess the point is that I enter not a login, but an email, but how fix this? how to make that there was an input on soap and the password? (standard user)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2019-08-03
@sergey-gornostaev

Write an authentication backend :

class EmailAuthenticationBackend(ModelBackend):
    def authenticate(self, request, email=None, password=None):
        try:
            user = User.objects.get(email=email)
            if user.check_password(password):
                return user
        except User.DoesNotExist:
            return None

    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None

_
_, 2019-08-03
@mrxor

To use email instead of username
1. Define your user model with Example:USERNAME_FIELD = 'email'

class User(AbstractUser):
    """User model."""
    
    username = None  # Можете выпилить username если он вам не нужен
    email = models.EmailField(_('email address'), unique=True)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

2. In settings.py add Related Links: One TwoAUTH_USER_MODEL = 'YOUR_APP.User'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question