***
VEIWS.PY This is views.py I am able to signin but not able to log in it shows invalid credentials
`def login_user(request): if request.method=='POST':
if request.POST.get('submit')=='sign_up':
username=request.POST.get('name')
email=request.POST.get('email')
password=request.POST.get('password')
if User.objects.filter(email=email).exists(): # Condition for same email id if already exists
messages.warning(request,'Email already exists')
else:
user =User(email=email,password=password,username=username)
user.set_password(password) #since raw passwords are not saved therefore needs to set in this method
user.save()
messages.success(request,'User has been registered successfully') #Dispalys message that user has been registerd
return redirect('login')
elif request.POST.get('loginsubmit')=='sign_in':
email = request.POST['email']
password = request.POST['password']
user = authenticate(request, email=email, password=password)
if user is not None:
login(request, user)
return redirect ('/')
else:
messages.warning(request,'Invalid credentials')
# print(email,password,username)
return render (request,'login.html')`
LOGIN.html
=======
`//<section id="form"><!--form-->
<div >
<div >
<div >
<div ><!--login form-->
<h2>Login to your account</h2>
<form method="POST" action="login">
{% csrf_token %}
<input type="email" placeholder="Email Address" id="validationDefault01" name="email" required>
<input type="password" placeholder="Password" id="validationDefault02" name="password" required>
<span>
<input type="checkbox" >
Keep me signed in
</span>
<button type="submit" name='loginsubmit' value='sign_in' >Login</button>
</form>
</div><!--/login form-->
</div>
<div >
<h2 >OR</h2>
</div>
<div >
{% block body %}
<body>
{% for message in messages %}
<section>
<div role="alert">
<strong>Message!</strong> {{message}}
<button type="button" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</div>
</section>
{% endfor %}
<section id="form"><!--form-->
<div >
<div >
<div >
<div ><!--login form-->
<h2>Login to your account</h2>
<form method="POST" action="login">
{% csrf_token %}
<input type="email" placeholder="Email Address" id="validationDefault01" name="email" required>
<input type="password" placeholder="Password" id="validationDefault02" name="password" required>
<span>
<input type="checkbox" >
Keep me signed in
</span>
<button type="submit" name='loginsubmit' value='sign_in' >Login</button>
</form>
</div><!--/login form-->
</div>
<div >
<h2 >OR</h2>
</div>
<div >
<div ><!--sign up form-->
<h2>New User Signup!</h2>
<form method="POST" action="login">
{% csrf_token %}
<input type="text" placeholder="Name" id="validationDefault05" name="name" required>
<input type="email" placeholder="Email Address" id="validationDefault03" name="email" required>
<input type="password" placeholder="Password" id="validationDefault04" name="password" required>
<button type="submit" name='submit' value='sign_up'>Signup</button>
</form>
</div><!--/sign up form-->
</div>
</div>
</div>
</section><!--/form-->
</body>
{% endblock %}
<div ><!--sign up form-->
<h2>New User Signup!</h2>
<form method="POST" action="login">
{% csrf_token %}
<input type="text" placeholder="Name" id="validationDefault05" name="name" required>
<input type="email" placeholder="Email Address" id="validationDefault03" name="email" required>
<input type="password" placeholder="Password" id="validationDefault04" name="password" required>
<button type="submit" name='submit' value='sign_up'>Signup</button>
</form>
</div><!--/sign up form-->
</div>
</div>
</div>
</section><!--/form-->//`
***URLS.py***
`
from django.contrib import admin
from django.urls import path
from .views import *
from . import views
urlpatterns = [
path('', views.index, name='index'),
# path('register',views.register, name='register'),
path('login',views.login_user, name='login'),
]
`
CodePudding user response:
it shows invalid credentials
Because your authenticate()
returns None
authenticate()
takes username and password by default for the authentication and you are providing email and password for authentication. If you want to authenticate from email and password write your own custom authentication backend.
from django.contrib.auth import get_user_model
from django.contrib.auth.backends import ModelBackend
class CustomEmailBackend(ModelBackend):
def authenticate(self, request, email=None, password=None, **kwargs):
UserModel = get_user_model()
try:
user = UserModel.objects.get(email=email)
except UserModel.DoesNotExist:
return None
else:
if user.check_password(password):
return user
return None
After that point your custom authentication in settings.py
AUTHENTICATION_BACKENDS = ['path.to.CustomEmailBackend']
Also make your email filed unique=True
CodePudding user response:
By default authentication is done by username field of the auth user model, you need to set username_field to email field in the auth user model.Your auth user model should be like this
class User(AbstractBaseUser,PermissionsMixin):
**fields**
USERNAME_FIELD = 'email'