<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')
Related
I did all the things according to the django.documentation, users are saved in database but still cannot log in homepage. Always bringing back to me that 'Bilgilerinizi Kontrol Ediniz' error message. Could someone please about what can be the reason?
Here is my "urls.py main":
urlpatterns = [
path('signup', views.signup, name='signup'),
path('login', views.login_user, name='login'),
path('', views.homepage, name='home')
]
Here "views.py":
def login_user(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('/')
else:
messages.info(request, 'Bilgilerinizi Kontrol Ediniz')
return redirect('login')
else:
return render(request, 'login.html')
And here it is my "login.html" page:
<form action="" method="post">
{% csrf_token %}
<div class="login-form">
<div>
{% for message in messages %}
<strong style="font-size: 0.8em;color:red">{{message}}</strong>
{% endfor %}
</div>
<div class="field">
<div class="control">
<input class="input email-input" name="email" type="text" placeholder="E-mail Adresiniz">
<div class="input-icon">
<i data-feather="user"></i>
</div>
</div>
</div>
<div class="field">
<div class="control">
<input class="input password-input" name="password" type="password" placeholder="●●●●●●●">
<div class="input-icon">
<i data-feather="lock"></i>
</div>
</div>
</div>
</div>
<div class="field is-flex" style="margin-top: 12px">
<div class="switch-block" style="display: flex;align-items: center;">
<label class="f-switch">
<input type="checkbox" class="is-switch">
<i></i>
</label>
<div class="meta">
<p style="font-size:.8rem">Beni Hatırla</p>
</div>
</div>
Şifremi Unuttum
</div>
<div class="field">
<div class="control">
<button type="submit" class="button is-solid primary-button raised is-rounded is-fullwidth">Giriş Yap</button>
</div>
</div>
</form>
And here is my database looks like:
Here it is how i save the passwords of users:
def signup(request):
if request.method == 'POST':
#acc_type = request.POST['acc_type']
firstname = request.POST['firstname']
lastname = request.POST['lastname']
email = request.POST['email']
password = request.POST['password']
passwordconf = request.POST['password-confirmation']
if password == passwordconf:
if User.objects.filter(email=email).exists():
messages.info(request, "Adınıza Kayıtlı Email Bulunmaktadır.")
return redirect('signup')
else:
#saving user to database
user = User.objects.create(username=firstname+' '+lastname, email=email, first_name=firstname, last_name=lastname, password=password )
user.save()
#creating profile object for the new user
user_model = User.objects.get(username=firstname+' '+lastname)
new_profile = Profile.objects.create(user=user_model, id_user=user_model.id)
new_profile.save()
return redirect('login')
else:
messages.info(request, 'Şifreleriniz Eşleşmiyor.')
return redirect('signup')
else:
return render(request, 'signup.html')
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)
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'
I have a problem with updating an existing user object in my database. When I update and change the user object it saves and everything is looking ok but when I change the user and I want to login this user does not authenticate. Here is my code:
def profile_edit(request):
user = request.user
profile = request.user.profile
context = {
'user': user
}
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
email = request.POST.get('email')
user.profile.address = request.POST.get('address')
user.profile.phone = request.POST.get('phone')
user.set_password('password')
user.username = username
user.save()
user.profile.save()
authenticate(user)
context = {
'profile': profile
}
return render(request, 'helpdesk/profile.html', context)
return render(request, 'helpdesk/profile_edit.html', context)
and login view:
def login_view(request):
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)
profile = request.user.profile
context = {
'profile': profile
}
return render(request, 'helpdesk/user_desk.html', context)
else:
context = {
'username': username,
'error': 'user not find!'
}
else:
context = {}
return render(request, 'helpdesk/login.html', context)
When updating the password you are actually setting the string 'password' as password and not the submitted password. Change user.set_password('password') to user.set_password(password) to correct this.
<form action="" style="font-size: 2cm" method="post">
{% csrf_token %}
<div class="row" style="margin: 7px">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<input type="text" name="username" id="first_name" class="form-control input-sm" placeholder="username:{{ request.user.username }}">
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<input type="text" name="address" id="last_name" class="form-control input-sm" placeholder="address:{{ request.user.address }}">
</div>
</div>
</div>
<div class="form-group">
<input type="email" name="email" id="email" class="form-control input-sm" placeholder="email: {{ request.user.email }}">
</div>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<input type="password" name="password" id="password" class="form-control input-sm" placeholder="Password">
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<input type="text" name="phone" id="password_confirmation" class="form-control input-sm" placeholder="phone:{{ request.user.phone }}">
</div>
</div>
</div>
<input type="submit" value="EDIT" class="btn btn-info btn-block" style="background-color: #0d0d0d; border-color: #0d0d0d">
</form>
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