Issues with forms.py - python

when I am submitting the form, I am getting KeyError at password1 in forms.py line 25 password1=self.cleaned_data['password1']. The code of files is given below:
The code of forms.py is:
from django import forms
from django.contrib.auth.models import User
from django.forms import ModelForm
from drinker.models import Drinker
class RegistrationForm(ModelForm):
username = forms.CharField(label=(u'User Name'))
email = forms.EmailField(label=(u'Email Address'))
password = forms.CharField(label=(u'Password'), widget=forms.PasswordInput(render_value=False))
password1 = forms.CharField(label=(u'Verify Password'), widget=forms.PasswordInput(render_value=False))
class Meta:
model=Drinker
exclude=('user',)
def clean_username(self):
username=self.cleaned_data['username']
try:
User.objects.get(username=username)
except User.DoesNotExist:
return username
raise forms.ValidationError("The Username is already taken, please try another.")
def clean_password(self):
password=self.cleaned_data['password']
password1=self.cleaned_data['password1']
if password != password1:
raise forms.ValidationError("The Password did not match, please try again.")
return password
The code of html file is:
{% extends "base.html" %}
{% block extrahead %}
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js" type="text/javascript"></script>
<script>
$(function() {
$( "#id_birthday" ).datepicker();
});
</script>
{% endblock %}
{% block content %}
<form action="" method="post">
{% csrf_token %}
{% if form.errors %}<p>Please correct the following fields:</p>{% endif %}
<div class="register_div">
{% if form.username.errors %}<p class="error">{{ form.username.errors }}</p>{% endif %}
<p><label for="username"{% if form.username.errors %} class="error"{% endif %}>Username:</label></p>
<p>{{ form.username }}</p>
</div>
<div class="register_div">
{% if form.email.errors %}<p class="error">{{ form.email.errors }}</p>{% endif %}
<p><label for="email"{% if form.email.errors %} class="error"{% endif %}>Email:</label></p>
<p>{{ form.email }}</p>
</div>
<div class="register_div">
{% if form.password.errors %}<p class="error">{{ form.password.errors }}</p>{% endif %}
<p><label for="password"{% if form.password.errors %} class="error"{% endif %}>Password:</label></p>
<p>{{ form.password }}</p>
</div>
<div class="register_div">
{% if form.password1.errors %}<p class="error">{{ form.password1.errors }}</p>{% endif %}
<p><label for="password1"{% if form.password1.errors %} class="error"{% endif %}>Password1:</label></p>
<p>{{ form.password1 }}</p>
</div>
<div class="register_div">
{% if form.birthday.errors %}<p class="error">{{ form.birthday.errors }}</p>{% endif %}
<p><label for="birthday"{% if form.birthday.errors %} class="error"{% endif %}>Birthday:</label></p>
<p>{{ form.birthday }}</p>
</div>
<div class="register_div">
{% if form.name.errors %}<p class="error">{{ form.name.errors }}</p>{% endif %}
<p><label for="name"{% if form.name.errors %} class="error"{% endif %}>Name:</label></p>
<p>{{ form.username }}</p>
</div>
<p><input type="submit" value="submit"/></p>
</form>
{% endblock %}
the view file is
from django.http import HttpResponseRedirect
from django.contrib.auth.models import User
from django.shortcuts import render_to_response
from drinker.models import Drinker
from django.template import RequestContext
from drinker.forms import RegistrationForm
def DrinkerRegistration(request):
if request.user.is_authenticated():
return HttpResponseRedirect('/profile/')
if request.method == 'POST':
#return render_to_response('register')
form = RegistrationForm(request.POST)
if form.is_valid():
user=User.objects.create_user(username=form.cleaned_data['username'], email=form.cleaned_data['email'], password = form.cleaned_data['password'])
user.save()
# drinker=user.get_profile()
# drinker.name=form.cleaned_data['name']
# drinker.birthday=form.cleaned_data['birthday']
# drinker.save()
drinker=Drinker(user=user,name=form.cleaned_data['name'],birthday=form.cleaned_data['birthday'])
drinker.save()
return HttpResponseRedirect('/profile/')
else:
return render_to_response('register.html',{'form':form} , context_instance=RequestContext(request))
else:
''' user is not submitting the form, show them a blank registration form '''
form = RegistrationForm()
context={'form':form}
return render_to_response('register.html',context , context_instance=RequestContext(request))

Django calls clean_xxx() methods for each field in the form. When a field is valid, it will add the key/value to cleaned_data. When Django calls clean_password and it has not yet processed the field password1 then cleaned_data won't contain a key/value pair for password1. That is the error you are currently getting.
To fix it, you need to move the validation of the passwords to clean() which is called after Django has checked each of the field individually. This method can be used to perform validation which concerns multiple fields of the form (i.e., the form as a whole).
This also makes sense because the error you want to raise is about the mismatch between password and password1, not just the field password (the field that you are currently cleaning).

Related

Django user login authentication

I'm want to log a user in, in simple web site.
i tried to create my own login authentication but it send this error:
AttributeError at /login/ , 'QuerySet' object has no attribute 'pk'
this is the code (views.py) :
...
from django.contrib.auth import login as login_module
def login(request):
if request.method == "POST":
Login = LoginFrom(request.POST)
if Login.is_valid():
user = Login.cleaned_data
if User.objects.filter(username=user['username']).exists():
user_info = User.objects.filter(username=user['username'] ,
password=user['password']).values('name')
if user_info.exists():
login_module(request, user_info)
messages.success(request, 'welcome {}'.format(user_info[0]['name'] ) , 'welcome_message')
else:
messages.error(request, 'wrong password', 'password_message')
else:
messages.error(request, 'username doesn\'t exist', 'username_message')
else:
Login = LoginFrom()
return render(request, 'login.html')
then i replace it with this code that's from django document; it get me into another problem which is always return 'None':
views.py
...
...
from django.contrib.auth import authenticate
from django.contrib.auth import login as login_module
def login(request):
if request.method == "POST":
Login = LoginFrom(request.POST)
if Login.is_valid():
cd = Login.cleaned_data
user = authenticate(request, username = cd['username'], password = cd['password'])
if user is not None:
login_module(request, user)
messages.success(request, 'login', 'welcome_message')
else:
messages.error(request, 'error (none)', 'error_message')
else:
Login = LoginFrom()
return render(request, 'login.html')
(form.py) in both way:
class LoginFrom(forms.Form):
username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput)
login.html:
<!DOCTYPE html>
<html>
<head><title>login</title></head>
{% include 'header.html' %}
<body class="background">
<form id="login_form" action="" method="POST">
{% csrf_token %}
<p class="lead">login to your account</p>
<div class="form-group">
<br>
<div class="input-group">
<div class="input-group-text" id="basic-addon1"><i class="bi bi-people"></i></div>
<input type="text" name="username" class="form-control" placeholder="Enter Your Username"required>
</div>
<div>
{% if messages %}
{% for message in messages %}
{% if 'username_message' in message.tags %}
<div class="text-danger" > {{message}} </div>
{% endif %}
{% endfor %}
{% endif %}
</div>
</div>
<div class="form-group">
<br>
<div class="input-group">
<div class="input-group-text" id="basic-addon1"><i class="bi bi-key"></i></div>
<input type="password" name="password" class="form-control" placeholder="Enter Your password" required>
</div>
<div>
{% if messages %}
{% for message in messages %}
{% if 'password_message' in message.tags %}
<div class="text-danger" > {{message}} </div>
{% endif %}
{% endfor %}
{% endif %}
</div>
</div>
<br>
<button class="btn btn-outline-secondary" type="submit" name="login" value="login" >Login</button>
<div>
{% if messages %}
{% for message in messages %}
{% if 'welcome_message' in message.tags %}
<div class="text-warning text-center" > {{message}} </div>
{% endif %}
{% endfor %}
{% endif %}
</div>
</form>
</body>
</html>
models.py
class User(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField()
username = models.CharField(max_length=100)
password = models.CharField(max_length=100)
comment_count = models.IntegerField(default=0)
did i missed some part or should set some setting?
In your first solution,
password=user['password']
will always fail as the password saved in default auth_user table will have the password hashed.
I tried the second solution and it is working fine. That login view cannot return None because the only return statement is returning render method.
Can you please share the login.html template.

Error message if I'm using IntegerField for one of the form field

I have an IntegerField in my login Form and it throws me an error of unsuccessful login message on submit. Although when I change IntegerField to StringField it works fine and validates the the login. Is there any way how to get IntegerField also validate on submit.
this is my route code
#app.route("/login", methods=['POST', 'GET'])
def login():
form = LoginForm()
if form.validate_on_submit():
if form.username.data == 'User' and form.code.data == '1234':
flash(f'Book your time {form.username.data}', 'secondary')
return redirect(url_for('booking'))
else:
flash('Login Unsuccessful. Please check your name and the code', 'danger')
return render_template('login.html', title='Login', form=form)
this is my forms.py file
from flask_wtf import FlaskForm
from wtforms import StringField, IntegerField, SubmitField
from wtforms.validators import DataRequired, Length, NumberRange
class LoginForm(FlaskForm):
username = StringField('Name', validators=[DataRequired(), Length(min=2, max=20)])
code = IntegerField('Code', validators=[DataRequired(), NumberRange(min=1, max=9999)])
submit = SubmitField('Book time')
this is my login.html file
<main class="form-signin">
<form method="POST" action="">
{{ form.hidden_tag() }}
<img class="mb-4" src="{{ url_for ('static', filename='img/vertical.png') }}" alt="" width="100">
<div class="form-floating">
{{ form.username.label(class="form-lable") }}
{% if form.username.errors %}
{{ form.username(class="form-control form-control-lg is-invalid") }}
<div class="invalid-feedback">
{% for error in form.username.errors %}
<span>
{{ error }}
</span>
{% endfor %}
</div>
{% else %}
{{ form.username(class="form-control") }}
{% endif %}
</div>
<div class="form-floating">
{{ form.code.label(class="form-lable") }}
{% if form.code.errors %}
{{ form.code(class="form-control is-invalid") }}
<div class="invalid-feedback">
{% for error in form.code.errors %}
<span>
{{ error }}
</span>
{% endfor %}
</div>
{% else %}
{{ form.code(class="form-control") }}
{% endif %}
</div>
<div class="form-floating">
{{ form.submit(class="w-100 btn btn-lg btn-primary") }}
</div>
<div class="bottom_link">
<a class="link-primary" href="{{url_for('booking')}}">See available bookings</a>
</div>
</form>
</main>

django wizard form problem in showing multiforms

My Views Page:
from django.shortcuts import render
from django.http import HttpResponse
from formtools.wizard.views import SessionWizardView
from .forms import ContactForm1,ContactForm2,ContactForm3
class ContactWizard(SessionWizardView):
template_name = 'contact.html'
form_list = [ContactForm1,ContactForm2]
def done(self, form_list, **kwargs):
form_data = process_form_data(form_list)
return render_to_response('done.html', {'form_data': form_data})
def process_form_data(form_list):
form_data = [form.cleaned_data for form in form_list]
return form_data
My form page:
from django import forms
class ContactForm1(forms.Form):enter code here
subject = forms.CharField(max_length=100)
class ContactForm2(forms.Form):
sender = forms.EmailField()
class ContactForm3(forms.Form):
message = forms.CharField(widget=forms.Textarea)
I am new to Django I am working with the wizard forms but this wizard form not showing the if statement of the wizard multiform. Please help me to solve the wizard form.
Html Page
{% load i18n %}
<p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
{% for field in form %}
{{field.error}}
{% endfor %}
<form action="/contact/" method="post">
{% csrf_token %}
<table>
{{ wizard.management_form }}
{% if wizard.form.forms %}
{{ wizard.form.management_form }}
{% for form in wizard.form.forms %}
{{ form }}
{% endfor %}
{% else %}
{{ wizard.form }}
{% endif %}
</table>
{% if wizard.steps.prev %}
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.first }}">{% trans "first step" %}</button>
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">{% trans "prev step" %}</button>
{% endif %}
<input type="submit" value="{% trans "submit" %}"/>
</form>
Help to find out the problem in wizard multiform

Incorrect redirect to user profile details

If the user is a staff' member he will see the list of users and their profile details, in addition to his profile details. If not, he will see only his profile details.
models.py
class UserProfile(AbstractUser):
def __str__(self):
return self.username
def get_absolute_url(self):
return reverse("user_details", kwargs={"username":self.username})
views.py
def usersList(request):
"""
The list of all users
"""
if request.user.is_superuser:
users_list = UserProfile.objects.all()
elif request.user.is_staff:
users_list = UserProfile.objects.filter(is_staff=False)
else:
raise PermissionDenied
template = 'usermanager/users_list.html'
context = {
'users': users_list,
}
return render(request, template, context)
def userDetails(request, username):
"""
User details
"""
user_details = get_object_or_404(UserProfile, username=username)
template = 'usermanager/user_details.html'
context = {
'user_details': user_details,
}
return render(request, template, context)
urls.py
path('user-list/', views.usersList, name='users'),
path('<username>/', views.userDetails, name='user_details'),
users_list.html
{% for user in users %}
<h1>{{ user.username }}</h1>
<p>Name: {{ user.first_name }}</p>
<p>Surname: {{ user.last_name }}</p>
<p>Email: {{ user.email }}</p>
{% if request.user.is_superuser %}
{% if user.is_staff %}
<button class="btn btn-warning">Staff</button>
{% else %}
<button class="btn btn-primary">User</button>
{% endif %}
{% endif %}
<a class="btn btn-danger" href="{% url 'user_details' username=user.username %}">Details</a>
<hr>
{% endfor %}
user_details.html
{% if request.user == user %}
<h1>Your profile</h1>
{% else %}
<h1>Profile of {{ user.username }}</h1>
{% endif %}
<hr>
<div class="container">
<p>Name: {{ user.first_name }}</p>
<p>Surname: {{ user.last_name }}</p>
<p>Email: {{ user.email }}</p>
{% if request.user.is_superuser %}
{% if user.is_staff %}
<button class="btn btn-warning">Staff</button>
{% else %}
<button class="btn btn-primary">User</button>
{% endif %}
{% endif %}
<p>Joined: {{ user.date_joined }}</p>
<p>Last login: {{ user.last_login }}</p>
{% if user.is_active %}
<button class="btn btn-success">Active</button>
{% else %}
<button class="btn btn-secondary">Inactive</button>
{% endif %}
</div>
I can see correctly the list of all users. Bob is one of my user, if I click on his details I see my personal profile. This happen for all of users details, I see always my profile. I'm logged in as superuser.
What I've wrong?
Your object for the user_Details is {{user_details}}, so you should be using that as a variable on the template:
{% if request.user == user_details %} and so on. That should solve your problem.
But as explained here in the docs, the template variable user is already used by Django to let you see the actual user information.
In your user list template you don't have this problem because you are overriding the user variable with the for.
Another recommendation is: Evade overriding the user variable on a template. for example on your users_list:
{% for usr in users %}
<h1>{{ usr.username }}</h1>
<p>Name: {{ usr.first_name }}</p>
<p>Surname: {{ usr.last_name }}</p>
<p>Email: {{ usr.email }}</p>
{% if request.user.is_superuser %}
{% if usr.is_staff %}
<button class="btn btn-warning">Staff</button>
{% else %}
<button class="btn btn-primary">User</button>
{% endif %}
{% endif %}
<a class="btn btn-danger" href="{% url 'user_details' username=usr.username %}">Details</a>
<hr>
{% endfor %}
It is my stupid error...
The contex of userDetails is user_details but I use user inside the template

How to raise a error inside form_valid method of a CreateView

In a Django project, I have a view (cloud), type: CreateView. This view has a inlineformset_factory. It works. But, if i submit the form with a error (look at "messages.error" below), the page is redirected to project.get_absolute_url(). The problem is: the form content back empty. I know thats because the HttpResponseRedirect.
How can I change this without to break the form?
views.py
class cloud(CreateView):
template_name = 'base/cloud.html'
form_class = UserForm
def get_context_data(self, **kwargs):
context = super(cloud, self).get_context_data(**kwargs)
project = get_object_or_404(Project, slug=self.kwargs['slug'])
context['project'] = project
if self.request.POST:
context['formset'] = IdeaFormset(self.request.POST or None)
else:
context['formset'] = IdeaFormset()
return context
def form_valid(self, form, **kwargs):
project = get_object_or_404(Project, slug=self.kwargs['slug'])
context = self.get_context_data()
formset = context['formset']
if formset.is_valid():
self.object = form.save()
formset.instance = self.object #IdeaFormFormSet
nouns = project.nouns().values_list('content', flat=True)
verbs = project.verbs().values_list('content', flat=True)
error = False
for form in formset.forms: #For each Idea
form.instance.project = project
if form.instance.sentence:
sentence = form.instance.sentence
validate_noun = [word for word in sentence.lower().split() if word in nouns]
validate_verbs = [word for word in sentence.lower().split() if word in verbs]
if (len(validate_noun) < 1):
error = True
messages.error(self.request, u'No noun was inserted into the sentence.', 'danger')
if (len(validate_verbs) < 1):
error = True
messages.error(self.request, u'No verb was inserted into the sentence.', 'danger')
if not error:
formset.save()
messages.success(self.request, u'Success!')
return HttpResponseRedirect( project.get_absolute_url() )
else:
return self.render_to_response(self.get_context_data(form=form))
cloud.html
<form role="form" method="post">
{% csrf_token %}
<legend>Ideas</legend>
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
{% for idea_form in formset %}
{# Include the hidden fields #}
{% for hidden in idea_form.hidden_fields %}
{{ hidden }}
{% endfor %}
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="heading_{{ forloop.counter }}">
<h4 class="panel-title">
<a {% if not forloop.first %} class="collapsed" {% endif %}
data-toggle="collapse" data-parent="#accordion" href="#collapse_{{ forloop.counter }}" aria-expanded="{{ forloop.first }}" aria-controls="collapse_{{ forloop.counter }}">
Idea #{{ forloop.counter }}
</a>
</h4>
</div>
<div id="collapse_{{ forloop.counter }}" class="panel-collapse collapse {% if forloop.first %} in {% endif %}" role="tabpanel" aria-labelledby="heading_{{ forloop.counter }}">
<div class="panel-body form-group" id="idea_{{ forloop.counter }}">
<div class="container-fluid">
{% for field in idea_form.visible_fields %}
<div class="row">
{{ field.errors }}
{{ field.label_tag }} {{ field }}
<span class="help-block">{{ field.help_text }}</span>
</div>
{% endfor %}
</div> <!--container-fluid-->
</div>
</div>
</div>
{% endfor %}
</div>
<legend>User</legend>
{% for field in form %}
<div class="form-group has-error">
{{ field.label_tag }} {{ field }}
{% if field.help_text %}
<p class="help-inline"><small>{{ field.help_text }}</small></p>
{% endif %}
<div class="help-block with-errors">
<span class="help-block">
{% for error in field.errors %}{{ error }}{% endfor %}
</span>
</div>
</div>
{% endfor %}
{{ formset.management_form }}
<div class="form-actions">
<button type="submit" class="btn btn-primary">Send!</button>
</div>
</form>
As a general rule, model validation should go into model fields validators or model's clean method. Form validation should go into form's clean or clean_<field> methods.
If you need to validate things in your view's form_valid, you can use form.add_error and then, instead of redirecting (or returning super(cloud, self).form_valid(form), which redirects anyways), you could return super(cloud, self).form_invalid(form).
Check:
https://docs.djangoproject.com/en/3.1/ref/forms/api/#django.forms.Form.add_error

Categories