Can't login in the view Django - django

i can't create view for login. I try to login as created superuser, but i can't.
I tried create user via admin panel, but i can't login too as that user.
login_view function:
def login_view(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)
print('successful')
else:
print('failed')
return render(request, 'login.html')
login.html:
<body>
{% if request.user.is_authenticated %}
<p>You are logged</p>
{% else %}
<form class="w-50 mx-auto" method="POST">
{% csrf_token %}
<div class="form-group">
<input type="email" class="form-control" placeholder="Enter email" name="email">
</div>
<div class="form-group">
<input type="password" class="form-control" placeholder="Enter password" name="password">
</div>
<button type="submit" class="btn btn-primary">Log in</button>
</form>
{% endif %}
</body>

input tag type should be username. And auth request should be like this authenticate(request, username=username, password=password)

Related

How can i Register and Login In The Same Page in Django?

i would like to have both my login form and my registration form on the same page within the same template, so i would like to have them under one view function but i am not too sure on how i can do that, here is my code.
Views.py
def register(request):
form = CreateUserForm()
if request.method == 'POST':
form = CreateUserForm(request.POST) == "Register"
if form.is_valid():
form.save()
user = form.cleaned_data.get('username')
messages.success(request,"Account was Created for " + user)
context = {'form':form}
return render(request,'login.html',context)
def login(request):
if request.method == "POST":
if request.POST.get('submit') == 'Login':
username = request.POST.get('username')
password = request.POST.get('password1')
user = authenticate(request, username=username, password=password)
if user is not None:
login(request,user)
return redirect('shop.html')
else:
messages.info(request, 'Wrong Username or password')
context = {}
return render(request,'shop.html',context)
login.html
<div class="formBx">
<form method="POST",name="Login",value="Login">
{% csrf_token %}
<h2>Sign In</h2>
{{form.username}}
{{form.password1}}
<input type="submit" name="submit" value="Login">
<p class="signup">Don't have an Account?Sign Up.</p>
{% for message in messages %}
<p id="messages">{{message}}</p>
{% endfor %}
</form>
</div>
</div>
<div class="user signUpBx">
<div class="formBx">
<form method="POST" value="Register">
{% csrf_token %}
<h2>Create an account</h2>
{{form.username}}
{{form.email}}
{{form.password1}}
{{form.password2}}
<input type="submit" name="submit" value="Register">
<p class="signup">Already Have an Account?Sign In.</p>
{% for message in messages %}
<p id="messages">{{message}}</p>
{% endfor %}
</form>
</div>
I'm getting AttributeError at /login/
'bool' object has no attribute 'is_valid' error right now.
You can use two separate forms for login and register in same view. Here is an example:
def register_login(request):
if "register" in request.method == "POST": #add the name "register" in your html button
..... your registration code
if "login" in request.method == "POST": #add the name "login" in your html button
..... your login code
**html**
<form>
{%csrf_token%}
.... your registration form
<button type="submit" name="register">register</button>
</form>
<form>
{%csrf_token%}
.... your login form
<button type="submit" name="login">register</button>
</form>

Django is not redirecting to profile page after login. What is wrong with my code?

This is my views.py
def LoginPage(request):
username = password = ''
next = ""
if request.GET:
next = request.GET['next']
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(request, username = username, password = password)
if user is not None:
login(request, user)
if next == "":
return HttpResponseRedirect('/Profile/')
else:
return HttpResponseRedirect(next)
context = {}
return render(request, 'login.html', context)
This is my template:
{% if next %}
<form class="" action='/Profile/' method="post">
{%else%}
<form class="" action="/Login/" method="post">
{% endif %} {% csrf_token %}
<p class="login-field-title"><strong>Username*</strong></p>
<input type="text" class="form-control col-lg-10 log-inp-field" placeholder="Enter Username" required>
<p class="login-field-title"><strong>Password*</strong></p>
<input type="password" class="form-control col-lg-10 log-inp-field" placeholder="Enter Password" required> {% for message in messages %}
<p id="messages">{{message}}</p>
{% endfor %}
<button type="submit" class="btn btn-dark btn-lg col-lg-10 log-btn">Log In</button>
</form>
I don't understand what I'm doing wrong. Some help would be very appreciated. Even after logging in with a registered user it is not redirecting me to the desired page and when i change the action, even with the un registered user it redirects me to the next page.
Add this to your settings file
LOGIN_REDIRECT_URL = 'YOUR_PROFILE_URL'

MultiValueDictKeyError, when I press sign up button

<div class="modal-signin" >
<h1>Login</h1>
<button id="exit_login">X</button>
<div class="form-for-signin">
<center>
<form id="login-form" method="post">
{% csrf_token %}
<label id="username-label">Username:</label><br>
<input class='username-input-field' id={{ form.username }} <br>
<label id="password-label">Password:</label><br>
<input class="password-input-field" id={{ form.password }} <br>
<button type="submit" class="submit-btn">Log in</button>
</form>
</center>
</div>
<div class="social-links">
<div id="social">
<a class="facebookBtn smGlobalBtn" href="#"></a>
<a class="twitterBtn smGlobalBtn" href="#"></a>
</div>
<div class="dont-have-an-account">
<p>Don't have an account?<button id="sign-up-button" style="font-size: inherit; outline: none; background: none; border: none">Sign up</button></p>
</div>
</div>
</div>
<div class="modal-signup">
<center>
<form method="post">
{% csrf_token %}
<h1>Sign up</h1>
<div class="inside-the-signup-form">
<label id="first-name-label">First name:</label><br>
<input id="{{ form_signup.first_name }}" required> <br>
<label id="last-name-label">Last name:</label><br>
<input id="{{ form_signup.last_name }}"><br>
<label id="username-label">Create Username:</label><br>
<input id="{{ form_signup.username }}"><br>
<label id="email-label">Email:</label><br>
<input id="{{ form_signup.email }}"><br>
<label id="createpassword-label">Create password:</label><br>
<input id="{{ form_signup.password }}"><br>
<label id="password2-label">Confirm password:</label><br>
<input id="{{ form_signup.password2 }}"><br>
</div>
<button type="submit">Sign Up</button>
</form>
</center>
views.py
def logging_in(request):
if request.method == 'POST':
form= LoginForm(request.POST)
username = request.POST['username']
password = request.POST['password']
user= authenticate(username=username, password=password)
if user is not None:
if form.is_valid():
login(request, user)
messages.success(request,'Logging in...')
else:
messages.error(request,'username or password is incorrect')
form= LoginForm()
else:
form = LoginForm()
return render(request, 'home.html', {'form': form})
def sign_up(request):
if request.method == 'POST':
form_signup = SignUpForm(request.POST)
if form_signup.is_valid():
password= request.POST['password']
password2= request.POST['password2']
if password != password2:
messages.error(request,'Password does not match')
form_signup= SignUpForm()
else:
form_signup.save()
else:
form_signup= SignUpForm()
else:
form_signup= SignUpForm()
return render(request, 'home.html', {'form_signup':form_signup})
I linked both of my views to the same page because I want my signup to be a modal (popup). If you have any suggestions on how I should change my code so when i press submit on my signup form, I don't get MultiValueDictKeyError at /'username'
Both forms contain basically what you expect. The signup has first_name, last_name, email, username, etc...
Please don't forget that I am creating a modal so the log in or sign up form should not affect the url.
Best way to avoid this, you can change
username = request.POST['username']
password = request.POST['password']
to this:
username = request.POST.get('username')
password = request.POST.get('password')

django custom login view (login as guest)

i am making an ecommerce and i have a checkout view in that view i have three forms : a login form a guest login form and a billing address form so basically if a user is logged in normally or as guest i am displaying the billing address form in the template 'checkout.html' otherwise i display the login form and the guest login form but these two forms are handled in different views checkout_login and guest_checkout_login so my question is : is it safe to do so ?
this is the checkout template :
{% if not billing_profile %}
<form method="post" action="{% url 'login_page' %}"> {% csrf_token %}
<input type="hidden" name="next" value="{{ request.build_absolute_uri }}">
{{ form }}
<button type="submit" class="btn btn-default">Submit</button>
</form>
<form method="post" action="{% url 'guest_login_page' %}"> {% csrf_token %}
<input type="hidden" name="next" value="{{ request.build_absolute_uri }}">
{{ guest_form }}
<button type="submit" class="btn btn-default">Submit</button>
</form>
{% else %}
<h1>checkout</h1>
billing profile:{{billing_profile}} </br>
<form method="post" action=".">{% csrf_token %}
{{ adress_form }}
<button type="submit" class="btn btn-default">Submit</button>
</form>
{% endif %}
{% endblock %}
this is the chekout_login view:
def login_page(request):
form = LoginForm(request.POST or None)
next_ = request.POST.get('next')
if request.method == 'POST':
if form.is_valid():
username = form.cleaned_data.get("username")
password = form.cleaned_data.get("password")
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
if is_safe_url(next_, request.get_host()):
guest_email_id = request.session.get('guest_email_id')
if guest_email_id:
del request.session['guest_email_id']
return redirect(next_)
else:
return redirect("/")
else:
form = LoginForm()
return redirect("/")
if i am doing any mistake please tell me

Login anonymous behaviour even though the user exist it shows invalid user

views.py
'
def user_login(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)
if user:
if user.is_active:
login(request, user)
return HttpResponseRedirect(reverse('index'))
else:
return HttpResponse("inactive")
else:
return HttpResponse("Someone tried to login and failed")
else:
return render(request, 'basic_app/login.html', {})
login.html
{% extends 'basic_app/base.html' %}
{% block content %}
<div class="jumbotron">
<form action="{% url 'basic_app:user_login' %}" method="POST">
{% csrf_token %}
<label for="username">USERNAME</label>
<input type = "text" name="Username" placeholder="enter">
<label for="password">Password</label>
<input type = "password" name="password" placeholder="PASSWORD">
<input type = "submit" name ="" value = "LOGIN">
</form>
</div>
{% endblock %}
In django admin the users are registered and are in database but when i login it shows Someone tried to login and failed .
I have to submit this by midnight need help noob here.