How to solve the error in login information - django

I have created a user and given him all the required data, but I cannot log into the page, I sure the email and password correct , but it gives me error in the information entered.
template:
<form action="" method="post" class="border rounded overflow-hidden p-5">
{% csrf_token %}
<h4 class="text-center py-3 title">Login </h4>
{% for message in messages %}
<div class="btn btn-sm btn-dark col-6" role="alert">
<h3 class="text-center"> {{ message }}</h3>
</div>
{% endfor %}
<div class="row">
<div class="form-group col-md-12 text-left px-0">
<label for="email">Email</label>
<input class="border rounded-2 p-3 bg-input " type="email" name="email" id="email" placeholder="Email ">
</div>
<div class="form-group col-md-12 text-left px-0">
<label for="password">Password </label>
<input class="border rounded-2 p-3 bg-input " type="password" class="form-control" id="password" name="password">
</div>
<button class="btn btn-sm btn-dark col-12 rounded-2 my-4 p-3" type="submit">Login </button>
</div>
</form>
my view:
from django.contrib.auth import authenticate,login
from django.shortcuts import render,redirect
from django.contrib.auth.models import auth,Group
from django.contrib import messages
from django.conf import settings
from django.contrib.auth.decorators import login_required
from .decorators import uthenticated_user
from app.models import *
# Create your views here.
#uthenticated_user
def login(request):
if request.method=='POST':
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('user_home')
else:
messages.error(request,'There was a problem logging in. Check your email and password or create an account')
return redirect('login')
else:
return render(request,'registration/sign-in.html')

As i know, authenticate is only taking username and password. You can try to find username by email firstly and then authenticate him:
#uthenticated_user
def login(request):
if request.method=='POST':
email=request.POST['email']
password=request.POST['password']
# New code
try:
username = User.objects.get(email=email).username
except User.DoesNotExist:
username = None
user=authenticate(request,username=username,password=password)
if user is not None:
login(request,user)
return redirect('user_home')
else:
messages.error(request,'There was a problem logging in. Check your email and password or create an account')
return redirect('login')
else:
return render(request,'registration/sign-in.html')

Related

Django doesn't requests the data to login users

<form action="login" method="post" >
{% csrf_token %}
<div class="form-group mb-3">
<label class="label" for="name">username</label>
<input type="text" id="username" class="form-control" required>
</div>
<div class="form-group mb-3">
<label class="label" for="password">password</label>
<input type="password" id="password" class="form-control" required>
</div>
<div class="form-group">
<input type="submit" action="login">
</div>
<div class="form-group d-md-flex">
<div class="w-50 text-left">
<label class="checkbox-wrap checkbox-primary mb-0">Remember Me
<input type="checkbox" checked>
<span class="checkmark"></span>
</label>
</div>
<div class="w-50 text-md-right">
Forgot Password
</div>
This is the form.
def login(request):
if request.method == "POST":
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect("dashboard")
else:
messages.info(request, "User doesn't exist. Contact the teacher!")
return redirect("index")
else:
return render(request, "login.html")
This is the views function.
from django.shortcuts import render, redirect
from django.contrib.auth.models import User, auth
from django.contrib import messages
from django.contrib.auth import authenticate, login
Those are the imports.
But this doesn't redirect user who has already registered, to the page specified. Not a problem of urls or the csrf_token. I think the problem is in the sending data part because the else function triggers and shows the massege user doesn't exist..... Please help.

Template not displaying Validation error (Django)

I'm trying to display the Validation Error in my template (register.html) but it's not displaying. What is wrong with this code?
and one more question "how I can display email already exist in this form"
I don't know how to display the "email already exist".
The Codes Goes here.
forms.py
from django.contrib.auth.models import User
from django import forms
from .models import *
from django.utils.translation import gettext as _
class RegisterForm(forms.ModelForm):
username = forms.CharField(widget=forms.TextInput())
password = forms.CharField(widget=forms.PasswordInput())
email = forms.CharField(widget=forms.EmailInput())
class Meta:
model = Customer
fields =["full_name", "username", "email", "password"]
def clean_username(self):
uname = self.cleaned_data.get('username')
if User.objects.filter(username = uname).exists():
raise forms.ValidationError(_('Customer with this username already exists'), code='invalid')
return uname
def __init__(self, *args, **kwargs):
super(RegisterForm, self).__init__(*args, **kwargs) # Call to ModelForm constructor
self.fields['username'].widget.attrs['style'] = 'width:500px; height:40px;'
self.fields['password'].widget.attrs['style'] = 'width:500px; height:40px;'
self.fields['email'].widget.attrs['style'] = 'width:500px; height:40px;'
self.fields['full_name'].widget.attrs['style'] = 'width:500px; height:40px;'
I can't display the Error Message on above code Customer with this username already exists
views.py
from django.shortcuts import render, redirect
from django.views.generic import CreateView, View, FormView
from django.contrib.auth import authenticate, login, logout
from django.urls import reverse_lazy
from .forms import *
# Create your views here.
class customerRegister(CreateView):
template_name = "register.html"
form_class = RegisterForm
success_url = reverse_lazy("main_app:base")
def form_valid(self, form):
username = form.cleaned_data.get("username")
email = form.cleaned_data.get("email")
password = form.cleaned_data.get("password")
user = User.objects.create_user(username, email, password)
form.instance.user = user
login(self.request, user)
return super().form_valid(form)
def get_success_url(self):
if "next" in self.request.GET:
next_url = self.request.GET.get("next")
return next_url
else:
return self.success_url
register.html
<body style="background-color: #95a5a6">
{% if error %}
<div class="alert alert-dismissible alert-danger">
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
<strong>Oh snap!</strong>{{form.non_field_errors}}
</div>
{% endif %}
<div class="container-register mx-auto">
<form action="{% url 'account:register' %}" method="post">
{% csrf_token %}
<fieldset>
<h2 class="text-center" style="margin-top: 50px">Register</h2>
<div class="form-group">
<label class="col-form-label mt-4" for="inputDefault">Full Name*</label><br>
{{form.full_name}}<br>
</div>
<div class="form-group">
<label class="col-form-label mt-4" for="inputDefault">Username*</label><br>
{{form.username}}<br>
</div>
<div class="form-group">
<label for="exampleInputEmail1" class="form-label mt-4 ">Email address*</label><br>
{{form.email}}<br>
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputPassword1" class="form-label mt-4">Password*</label><br>
{{form.password}}<br>
</div>
<br/>
<div class="text-center">
<button type="submit" class="btn btn-lg btn-primary">Register</button>
</div>
</fieldset>
</form>
<hr>
<h5 class="text-center" style="margin-top: 50px">Already Registered?<a class="nav-link" href="{% url 'account:login'%}">Log in</a></h5>
</div>
</body>
In your template you have {% if error %} which does not exist as you are not passing the template context error. Inside the template if you are then accessing {{form.non_field_errors}} which will exist if there are errors.
So it should be {% if form.non_field_errors %} like so:
{% if form.non_field_errors %}
<div class="alert alert-dismissible alert-danger">
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
<strong>Oh snap!</strong>{{form.non_field_errors}}
</div>
{% endif %}
See related StackOverflow post here for customising the email error message.

Issues with two forms submission in django one after another

I'm working on forget password page in which the user first have to answer the question for enabling the textfields for creating new password.
Here, I have two forms, One for security question and second for password and confirm password.
Following is my forms.py
from django import forms
from .models import SecurityQuestions
class PasswordForm(forms.Form):
password = forms.CharField(disabled=True, widget=forms.PasswordInput(attrs={'placeholder':'New Password'}))
password_confirm = forms.CharField(disabled=True, widget=forms.PasswordInput(attrs={'placeholder':'Re-enter Password'}))
def clean(self, *args,**kwargs):
password = self.cleaned_data.get('password')
password_confirm = self.cleaned_data.get('password_confirm')
if password and password_confirm:
if password != password_confirm:
raise forms.ValidationError('Password Mismatch')
return super(PasswordForm, self).clean(*args, **kwargs)
class PasswordVerificationForm(forms.Form):
question = forms.ModelChoiceField(queryset=SecurityQuestions.objects.all(), empty_label=None, widget=forms.Select(attrs={'class':'form-control','id': 'sectxt'}))
answer = forms.CharField(label='answer', widget=forms.TextInput(attrs={'placeholder':'Answer','id': 'anstxt'}))
Following is my views.py
from django.shortcuts import render, redirect
from .forms import PasswordForm, PasswordVerificationForm
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth.hashers import make_password
from .models import SecurityQuestions
from django.contrib import messages
#login_required
#csrf_exempt
def password_reset(request):
form = PasswordForm(request.POST or None)
form1 = PasswordVerificationForm(request.POST or None)
if request.method == 'POST':
if request.POST.get("verify", False):
question = request.POST.get('question')
answer = request.POST.get('answer')
print("question",question)
print("answer",answer)
check = SecurityQuestions.objects.get(id=question) #id=1
print(check.answer)
if check.answer == answer:
messages.success(request, 'Enter Your New Password', 'alert-success')
form.fields['password'].disabled = False
form.fields['password_confirm'].disabled = False
else:
redirect('/')
messages.error(request, 'Incorrect Answer', 'alert-danger')
if request.POST.get("create", False):
if form.is_valid():
print("For Changing Password...")
password = form.cleaned_data.get('password')
request.user.password = make_password(password)
request.user.save()
return redirect('/')
else:
form = PasswordForm()
form1 = PasswordVerificationForm()
return render(request,"forget_password.html", {"form": form, "form1":form1})
Following is my forget_password.html
<div class="container">
<div class="main">
<div class="row justify-content-center">
<div class="col-md-4">
<div class="login-form">
<div class="row">
<div class="col-md-12">
<div class="login-title-holder">
<h4>Forgot Password</h4>
</div>
</div>
<form method="post">
<div class="form-group col-md-12">
<div class="input-group">
{{ form1.question | add_class:'form-control' }}
<span class="input-group-append">
<div class="input-group-text input-group-icon"><i class="fa fa-question" aria-hidden="true"></i></div>
</span>
</div>
</div>
<div class="form-group col-md-12">
<div class="input-group">
{{ form1.answer | add_class:'form-control' }}
<span class="input-group-append">
<div class="input-group-text input-group-icon "><i class="fa fa-comment" aria-hidden="true"></i></div>
</span>
</div>
</div>
<div class="col-md-12">
{% if messages %}
{% for message in messages %}
<div {% if message.tags %} class="alert {{ message.tags }} text-center"{% endif %}>
×
{{ message }}
</div>
{% endfor %}
{% endif %}
<input type="submit" name = "verify" formmethod="post" style="visibility: hidden;">
</div>
</form>
<form method="post">
<div class="form-group col-md-12">
<div class="input-group">
{{ form.password | add_class:'form-control' }}
<span class="input-group-append">
<div class="input-group-text input-group-icon"><i class="fa fa-key" aria-hidden="true"></i></div>
</span>
</div>
</div>
<div class="form-group col-md-12">
<div class="input-group">
{{ form.password_confirm | add_class:'form-control' }}
<span class="input-group-append">
<div class="input-group-text input-group-icon"><i class="fa fa-key" aria-hidden="true"></i></div>
</span>
</div>
</div>
<div class="col-md-12">
<div class="button-holder">
Cancel
<button class="login-btn" type="submit" formmethod="post" name="create">Create</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
If I enter the security answer first, based on the condition, if true, it enables the textfields for password and password_confirm.
But it's not creating the new password.
However, if I change the disabled = False in PasswordForm then it creating the new password successfully.
I want to know why it's not executing the code after the first form executes successfully.
Thanks!
You really should chain this into 2 urls, rather then trying 2 forms in one page. You can only submit one form and this is the problem you're facing. Once you have submitted the security question, you instantiate the form again with fields disabled:
form = PasswordForm(request.POST or None)
And now they do not get enabled, because the submit button called 'verify' from form1 is no longer present, so the code in that branch is not executed.
Let's say url is /password_reset/ - a rough outline (untested):
#login_required
#csrf_exempt
def security_question(request):
form = PasswordVerificationForm(request.POST)
if request.method == 'POST':
if form.is_valid():
token = generate_strong_token() # Implement: generate a strong token, url safe
request.session["password_reset_token"] = token
return redirect(f'/password_reset/{token}/')
else:
return render(...)
#login_required
#csrf_exempt
def change_password(request, **kwargs):
form = PasswordForm(request.POST)
token = request.session.get('password_reset_token')
if token == kwargs['token']:
if request.method == 'POST' and form.is_valid():
del request.session['password_reset_token']
# handle password change and redirect to wherever
else:
return render(...)
else:
raise SecurityError('Invalid token')
Your urls would be something like:
urlpatterns = [
re_path('password_reset/(?P<token>[0-9A-F]{32})/', change_password)
path('password_reset/', security_question)
]

Unexpexted MultiValueDictKeyError in signup code

I am new to Django. I am creating a signup page. But I am having an unexpected MultiValueDictKeyError with Exception value fname.
views.py:
from django.shortcuts import render, redirect
from django.contrib.auth.models import User
from django.contrib import auth
# Create your views here.
def signup(request):
if request.method == 'POST':
# User has and wants account now
if request.POST['password1'] == request.POST['password2']:
# Check if username already exists
try:
user = User.objects.get(username=request.POST['uname'])
return render(request, 'accounts/signup.html',{'error':'username already exist'})
# If username unoccupied
except User.DoesNotExist:
user = User.objects.create_user(fname = request.POST['fname'],lname = request.POST['lname'],email = request.POST['email'],uname = request.POST['uname'],password = request.POST['password1'])
# updating new user
auth.login(request,user)
return redirect('home')
else:
return render(request,'accounts/signup.html')
def login(request):
if request.method == 'POST':
#
user = auth.authenticate(username = request.POST['username'],password = request.POST['password'])
if user is not None:
auth.login(request,user)
return redirect('home')
else:
return render(request, 'accounts/login.html',{'error': 'username or password incorrect'})
else:
#
return render(request,'accounts/login.html')
def logout(request):
if request.method == 'POST':
auth.logout(request)
return redirect('home')
sign up page:
{% extends 'base.html'%}
{% block content %}
<div class="container-fluid" style="background-image: linear-gradient(to right, #1a1aff , #000099);padding: 10vh">
<div class="row">
<div class="col center" style="color: white;padding: 05vw">
<h1> Sign Up Now! </h1>
<br>
<h2> Become the part world's first personalized <br> Network <h2>
</div>
<div class="col-5 center container" style="color:black;padding: 02vw;background-color: white;">
<span>
<center>
<h1>Sign Up</h1>
</center>
<br>
</span>
<form action="{% url 'signup' %}" method="POST">
{% csrf_token %}
{% if error %}
<p style="color:red "> {{error}} </p>
{% endif %}
<h3>First Name</h3>
<input type="text" id="fname" name="firstname" placeholder="Your name..">
<br>
<h3>Last Name</h3>
<input type="text" id="lname" name="lastname" placeholder="Last Name">
<br>
<h3>Email</h3>
<input type="email" id="email" name="email" placeholder="Email Address">
<br>
<h3>Username</h3>
<input type="text" id="uname" name="uname" placeholder="Username">
<br>
<h3>Password</h3>
<input type="password" id="password" name="password1" placeholder="Password">
<br>
<h3>Confirm Password</h3>
<input type="password" id="password" name="password2" placeholder="Password">
<br>
<br>
<input type="submit" value="Sign Up Now">
</form>
</div>
</div>
</div>
enter code here
{% endblock %}
The field is firstname, not fname. This is why you should use Django forms rather than accessing the POST directly

Django Authentication (Custom Login Page)

Im trying to write my own django login page. Eveything is ok up to the point where I try to login and I just get redirected back to the main login view.
TEMPLATE
{% extends "bdayremind-maininput.html" %}
{% block main %}
<form class="form-horizontal" name="LoginForm" action="/login/" method="post">
{% csrf_token %}
{% if next %}
<input type="hidden" name="next" value="{{ next }}" />
{% endif %}
<div class="control-group">
<label class="control-label" for="username">Username</label>
<div class="controls">
<input type="text" id="username" value="{{username}}" placeholder="Username">
</div>
</div>
<div class="control-group">
<label class="control-label" for="password">Password</label>
<div class="controls">
<input type="password" name="password" id="password" placeholder="Password">
</div>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" class="btn">Login</button>
</div>
</div>
</form>
{% endblock %}
URLS
urlpatterns = patterns('',
url(r'^bdayremind_maininput/$', 'birthdayreminder.views.main'),
# login / logout
(r'^login/$', 'birthdayreminder.views.login_user'),
#(r'^logout/$', logout_page),
)
VIEWS
import datetime
from django.http import *
from django.shortcuts import render_to_response
from django.template import RequestContext
from birthdayreminder.models import *
from django.contrib.auth.decorators import login_required
from django.contrib.auth import authenticate, login
def login_user(request):
username = password = ''
if request.POST:
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return HttpResponseRedirect('birthdayreminder.views.main')
return render_to_response('login.html',{'username': username}, context_instance=RequestContext(request))
#login_required(login_url='/login/')
def main(request):
........
Any Ideas ?
You forgot to add redirect to the desired page after login.