Answer the question
In order to leave comments, you need to log in
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>
path('login/', views.LoginView.as_view(), name='login')
Answer the question
In order to leave comments, you need to log in
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
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 = []
AUTH_USER_MODEL = 'YOUR_APP.User'
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question