My problem is, when I validate forms for errors, some text with warning would show up, But id does not. I was searching for some good answers and I tried also everything.
I was trying to implement validation in my register subpage so....
First is my model.py for registration
from django.db import models
class User(models.Model):
name = models.CharField(max_length=30)
email = models.EmailField(max_length=30)
password = models.CharField(max_length=30)
def __str__(self):
return "Login: " + self.name + \
" | e-mail: " + self.email + \
" | password: " + self.password
Second is my inputForms.py with simple validation
from django import forms
from django.contrib.auth.models import User
from django.contrib import messages
from .models import User
class NewUser(forms.Form):
name = forms.CharField(required=True)
email = forms.EmailField(required=True)
password = forms.CharField(widget=forms.PasswordInput, required=True)
pic1 = forms.CharField(required=True)
pic2 = forms.CharField(required=True)
pic3 = forms.CharField(required=True)
pic4 = forms.CharField(required=True)
pic5 = forms.CharField(required=True)
class Meta:
model = User
fields = ('name', 'email', 'password')
def clean_email(self):
email_passed = self.cleaned_data.get('email')
email_req = "yourdomain.com"
if not email_req in email_passed:
raise forms.ValidationError("Not a valid email. Please try again")
return email_passed
Third is my view.py
from django.http import HttpResponseRedirect
from django.shortcuts import render
from definitions import SITE_ROOT
from .models import User
from . import inputForms
from threading import Thread
import base64
import re
import os
def index(request):
if request.method == 'GET':
return render(request, 'register.html', {'NewUser': inputForms.NewUser})
else:
return HttpResponseRedirect('/Kniznica')
def new_user(request):
if request.method == 'POST':
form = inputForms.NewUser(request.POST)
if form.is_valid():
user_name = form.cleaned_data['name']
user_email = form.cleaned_data['email']
user_password = form.cleaned_data['password']
thread = Thread(target=save_user, args=(form, user_name, user_email, user_password, ))
thread.start()
return HttpResponseRedirect('/Kniznica')
else:
return render(request, "register.html", {'NewUser': inputForms.NewUser})
def save_user(form, user_name, user_email, user_password):
base_to_png(form.cleaned_data['pic1'], user_name, "face1.png")
base_to_png(form.cleaned_data['pic2'], user_name, "face2.png")
base_to_png(form.cleaned_data['pic3'], user_name, "face3.png")
base_to_png(form.cleaned_data['pic4'], user_name, "face4.png")
base_to_png(form.cleaned_data['pic5'], user_name, "face5.png")
user_instance = User.objects.create(name=user_name, email=user_email, password=user_password)
user_instance.save()
def base_to_png(base64_string, name, image_name):
string_without_header = re.sub('^data:image/.+;base64,', '', base64_string)
img_data = base64.b64decode(string_without_header)
if not os.path.exists(SITE_ROOT + "/faceId/" + name):
os.makedirs(SITE_ROOT + "/faceId/" + name)
filename = SITE_ROOT + "/faceId/" + name + "/" + image_name
with open(filename, 'wb') as f:
f.write(img_data)
os.chdir(SITE_ROOT + "\static\scripts")
os.system("node face-detection.js " + name + " " + image_name)
And fourth is my template.html for registration
<div>
<form action="newUser" method="post" novalidate>
{% csrf_token %}
{{ form.non_field_errors }}
{{ form.username.errors }}
{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<p> {{ errors }} </p>
{% endfor %}
{% endfor %}
{% endif %}
<div style="display: inline-block">
Meno:{{ NewUser.name }}
</div>
<div style="display: inline-block">
Email:{{ NewUser.email }}
</div>
<div style="display: inline-block">
Heslo:{{ NewUser.password }}
</div>
<div hidden>
{{ NewUser.pic1 }}
{{ NewUser.pic2 }}
{{ NewUser.pic3 }}
{{ NewUser.pic4 }}
{{ NewUser.pic5 }}
</div>
<div>
<video id="video" width="300" height="300" autoplay></video>
</div>
<div>
<p id="counter"></p>
<button type="button" id="snap">Snap Photo</button>
<button type="submit" id="save" name="register">Registrácia</button>
</div>
<canvas hidden id="canvas" width="640" height="480"></canvas>
</form>
</div>
You have an unusual view structure here - normally you would use the same view for displaying the initial form as you would for processing the submission and displaying errors.
But nevertheless, you do have a couple of obvious issues. Firstly, in your new_user view, when the form is not valid you don't send that invalid form back, but instead send the form class itself. You should always send an instance of the form anyway, and in this case you should send back the same instance you have already validated, because that's the thing with the errors:
return render(request, "register.html", {'NewUser': form})
Secondly, in your template, you are inconsistent about what you call the form. When you render the fields, you access it via NewUser. But in trying to display the errors, for some reason you call it form. As you have used the name NewUser when you send it from the view, you should use that to display the errors:
{{ NewUser.errors }}
Related
When I try to click on login button it always execute the invalid credentials instead of redirect to the index page.. What I did is that in database create table name signup and wants to validate all the data from that table.. Here signup_data function is works well but in login_data cannot authenticate the user.
Models.py
from django.db import models
class signup(models.Model):
username = models.CharField(max_length=10)
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)
email = models.EmailField()
password = models.CharField(max_length=10)
Forms.py
from django.forms import ModelForm
from . models import signup
from django import forms
class signupform(ModelForm):
username= forms.CharField(max_length=10,widget=forms.TextInput(attrs={'class':'form-control'}))
first_name = forms.CharField(max_length=20, widget=forms.TextInput(attrs={'class': 'form-control'}))
last_name = forms.CharField(max_length=20,widget=forms.TextInput(attrs={'class': 'form-control'}))
email = forms.EmailField(max_length=20,widget=forms.EmailInput(attrs={'class': 'form-control'}))
password = forms.CharField(max_length=10,widget=forms.PasswordInput(attrs={'class':'form-control'}))
class Meta:
model = signup
fields = '__all__'
Views.py
from django.shortcuts import render,redirect
from . forms import signupform
from . models import signup
from django.contrib import messages
from django.contrib.auth import login,authenticate
def index(response):
return render(response,'login_module/index.html')
def signup_data(response):
if response.method == 'POST':
form = signupform(response.POST)
if form.is_valid():
username = form.cleaned_data['username']
first_name = form.cleaned_data['first_name']
last_name = form.cleaned_data['last_name']
email = form.cleaned_data['email']
password = form.cleaned_data['password']
if signup.objects.filter(username=username).exists():
# messages.add_message(response,messages.WARNING,'Username is already taken')
messages.error(response,'Username is already taken')
return redirect('signup')
elif signup.objects.filter(email=email).exists():
messages.error(response,'Email is already taken')
# messages.add_message(response,messages.WARNING,'Email is already taken')
return redirect('signup')
else:
register_instance = signup(username=username, first_name=first_name, last_name=last_name, email=email, password=password)
register_instance.save()
messages.success(response,'Registered Successfull')
return redirect('signup')
else:
form = signupform()
return render(response,'login_module/signup.html',{'form':form, 'message': messages})
def login_data(request):
form = signupform(request.POST or None)
if request.method == "POST":
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
login(request,user)
return redirect('index')
else:
messages.error(request,'Invalid Credentials')
return redirect('login')
else:
return render(request,'login_module/login.html',{'form':form, 'message': messages})
Login.html
{% extends 'login_module/base.html' %}
{% block content %}
<div class="container">
<div class="row">
<div class="col-md-12 text-center mt-50">
<h1>Login Page</h1>
</div>
</div>
<div class="row">
<div class="col-md-4 offset-md-4 ">
<form method="post" action="{% url 'login' %}">
{% csrf_token %}
<div class="form-group rounded-top">
{{ form.username.label_tag }} {{ form.username }}
</div>
<div class="form-group rounded-top">
{{ form.password.label_tag }} {{ form.password }}
</div>
<div class="text-center">
<button type="submit" class="btn btn-primary">Login</button>
</div>
</form>
</div>
</div>
<div class="text-center mt-50">
{% if messages %}
{% for message in messages %}
<div class="alert alert-danger" role="alert">
{{ message }}
{% endfor %}
{% endif %}
</div>
</div>
{% endblock %}
Login Page
As far as I can tell you are creating a signup object when signing up, but when trying to login with authenticate django is looking for the generic User model. You have to create a User in the database rather than a signup object to use authenticate.
I've been trying different ways to send an attachment from a submitted form but I'm not able to get it right.
my model:
from django.db import models
class Document(models.Model):
description = models.CharField(max_length=255, blank=True)
document = models.FileField()
uploaded_at = models.DateTimeField(auto_now_add=True)
views.py
from django.shortcuts import render, redirect
from django.core.mail import EmailMessage
from decouple import config
from .forms import ContactForm
def contact(request):
contact_form = ContactForm()
if request.method == "POST":
contact_form = ContactForm(request.POST, request.FILES)
if contact_form.is_valid():
name = request.POST.get("name")
email = request.POST.get("email")
subject = request.POST.get("subject")
message = request.POST.get("message")
upload = request.POST.get("upload")
email_content= EmailMessage("Message from django-structure project",
f"The user with name {name}, with the email {email},"
f" write the following: \n Subject: {subject} \n File{upload} \n\n Message: {message}",
"", [config('EMAIL_TO')], reply_to=[email])
try:
email_content.send()
return redirect("/contact/?valid")
except:
return redirect("/contact/?invalid")
return render(request, "contact.html", {"form": contact_form})
contact.html
<div class="row">
<div class="col">
<div class="form-group">
{% if "valid" in request.GET %}
<p style="color: blue;">Information sent successfully </p>
{% endif %}
<form action="" method="POST" enctype="multipart/form-data" style="text-align: center;">
{% csrf_token %}
<table style="color: dark; margin:20px auto;">{{form.as_table}}</table>
<input type="submit" value="Send" style="width: 150px;">
</form>
</div>
When I submit the form, I receive the message but in the content of the File, just the following string: "FleNone". (I have attached a pdf)
I am trying to create a student register page that allows the student to upload a profile photo. I am using Django User model and a StudentProfile model that has a OneToOne relation with User. Here are my codes:
student\models.py:
from django.db import models
from django.contrib.auth.models import User
class StudentProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE,)
avatar = models.ImageField(upload_to='student_profile/', null=True, blank=True)
def __str__(self):
return self.user.username
students/form.py:
from django import forms
class ImageUploadForm(forms.Form):
profile_photo = forms.ImageField()
eLearning/views.py:
from django.contrib.auth import authenticate, login, get_user_model
from django.http import HttpResponse
from django.shortcuts import render, redirect
from .forms import LoginForm, RegisterForm
from students.forms import ImageUploadForm
from students.models import StudentProfile
User = get_user_model()
def register_page(request):
register_form = RegisterForm(request.POST or None)
photo_upload_form = ImageUploadForm(request.POST, request.FILES)
context = {
"register_form": register_form,
"photo_upload form": photo_upload_form
}
if register_form.is_valid():
# print(register_form.cleaned_data)
username = register_form.cleaned_data.get("username")
first_name = register_form.cleaned_data.get("first_name")
last_name = register_form.cleaned_data.get("last_name")
email = register_form.cleaned_data.get("email")
password = register_form.cleaned_data.get("password")
new_user = User.objects.create_user(
username, email, password,
first_name=first_name,
last_name=last_name,
)
if photo_upload_form.is_valid():
user = username
avatar = photo_upload_form.cleaned_data.get("profile_photo")
new_user_profile = StudentProfile.objects.create(user, avatar)
print(new_user)
return render(request, "auth/register.html", context)
auth/register.html:
{% extends "base.html" %}
{% load static %}
{% block content %}
{% load crispy_forms_tags %}
<div class="container">
<div class="row my-4">
<div class="col-5">
<form action="" method="post" class="form-control">
{% csrf_token %}
{{ register_form|crispy }}
<input type="submit" class="btn btn-default" value="Submit">
</form>
</div>
<div class="col-5">
<form method="post" enctype="multipart/form-data" class="form-control">
{% csrf_token %}
<input id="id_image" type="file" class="my-2" name="image">
{{ photo_upload_form|crispy }}
</form>
</div>
</div>
</div>
{% endblock %}
I am facing 2 problems:
1) The ImageUploadForm is not rendering on to register.html
2) A StudentProfile is not being created. User is being created fine.
I also tried replacing form with ModelForm for ImageUploadForm but I get a NULL constraint for student_user since Django doesn't what user for StudentProfile is.
I have been looking through Stack Overflow. All solutions are about how to upload a user image to Django admin but I haven't found anything that shows how to associate the uploaded image to User model during registration. Forgive me if this is a repeated question. Thanks.
In your eLearning/views.py:
the context you are passing to the html page the _ is missing
context = {
"register_form": register_form,
"photo_upload form": photo_upload_form
}
This will be the reason for the ImageUploadForm is not rendering on to register.html
It's should be like
context = {
"register_form": register_form,
"photo_upload_form": photo_upload_form
}
So I figured it out. The real issue was with the register.html code. The submit button only worked for the register_form so the photo upload form was not validating, hence student profile entry was not being created. Here's the updated code:
eLearning/views.py:
from students.forms import ImageUploadForm
from students.views import upload_pic
def register_page(request):
register_form = RegisterForm(request.POST or None)
photo_upload_form = ImageUploadForm(request.POST, request.FILES)
context = {
"register_form": register_form,
"photo_upload_form": photo_upload_form
}
if register_form.is_valid():
username = register_form.cleaned_data.get("username")
first_name = register_form.cleaned_data.get("first_name")
last_name = register_form.cleaned_data.get("last_name")
email = register_form.cleaned_data.get("email")
password = register_form.cleaned_data.get("password")
new_user = User.objects.create_user(
username, email, password,
first_name=first_name,
last_name=last_name,
)
upload_pic(request, photo_upload_form, username=username)
return render(request, "auth/register.html", context)
students/views.py:
from django.contrib.auth import get_user_model
from django.http import HttpResponseRedirect
from django.shortcuts import render, redirect, HttpResponse
from .models import StudentProfile
from .forms import ImageUploadForm
def upload_pic(request, form, username):
if request.method == 'POST':
if form.is_valid():
User = get_user_model()
user = User.objects.get(username=username)
avatar = form.cleaned_data.get('profile_photo')
new_user_profile = StudentProfile.objects.create(user=user, avatar=avatar)
new_user_profile.save()
register.html:
{% extends "base.html" %}
{% load static %}
{% block content %}
{% load crispy_forms_tags %}
<div class="container">
<div class="row my-4">
<div class="col-5">
<form action="" method="post" enctype="multipart/form-data" class="form-control">
{% csrf_token %}
{{ register_form|crispy }}
{{ photo_upload_form|crispy }}
<input type="submit" class="btn btn-default" value="Submit">
</form>
</div>
</div>
</div>
{% endblock %}
You have to make sure enctype="multipart/form-data" is inside your tags or the image upload form will not get validated. I would also recommend adding an image validation method to your form.py. Something Sachin pointed out earlier: image form and validation. Hope this helps.
When I try to test my form with an email which has already been entered into the database it doesn't give the error message like it should, it redirects back to the homepage.
My views.py file looks like this:
from django.shortcuts import render
from .forms import LotteryForm
from django.http import HttpResponseRedirect
# Create your views here.
def lottery_list(request):
return render(request, 'lottery/lottery.html', {})
def lottery_new(request):
if request.method == 'POST':
form = LotteryForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('lottery_submitted')
else:
return render(request, 'lottery/lottery.html' {'form': LotteryForm()})
else:
form = LotteryForm()
return render(request, 'lottery/lottery.html', {'form': LotteryForm()})
My form is:
from django import forms
from django.db import models
from django.forms import ModelForm
from .models import Lottery
from .models import Lottery_user
from django.core.exceptions import ValidationError
class LotteryForm(forms.ModelForm):
lottery_type = forms.ModelChoiceField(queryset = Lottery.objects.all(), empty_label=None)
first_name = forms.CharField(required=True)
last_name = forms.CharField(required=True)
lottery_numbers = forms.CharField(max_length=12, required=True)
email = forms.EmailField(required=True)
telephone = forms.CharField(max_length=18,
error_messages={'invalid':'Enter a valid mobile number',
'required':'Enter a valid mobile number'})
def clean_email(self):
email = self.cleaned_data['email']
if Lottery_user.objects.filter(email=email).exists():
raise ValidationError("Email already exists")
return email
if Lottery_user.objects.filter(email=self.cleaned_data['email']).exists():
raise ValidationError("You've already entered")
return email
class Meta:
model = Lottery_user
fields = ['lottery_numbers', 'lottery_type', 'first_name', 'last_name', 'email', 'telephone',]
Template form:
<form action="{% url 'lottery_new' %}" method="post">
{% csrf_token %}
<div class="fieldWrapper">
<label for="lotterytype">Choose a lottery:</label>
{{ form.lottery_type }}
<p>See <a href=lottery_instructions>instructions and rules</a></p>
</div>
<div class="fieldWrapper">
<label for="lotterynumbers">Lottery Numbers:</label>
{{ form.lottery_numbers }}
</div>
<div class="fieldWrapper">
<label for="firstname">First name:</label>
{{ form.first_name }}
</div>
<div class="fieldWrapper">
<label for="lastname">Last name:</label>
{{ form.last_name }}
</div>
<div class="fieldWrapper">
{{ form.email.errors }}
{{ form.email.non_field_errors }}
<label for="email">Email:</label>
{{ form.email }}
</div>
<div class="fieldWrapper">
<label for="phonenumber">Telephone Number:</label>
{{ form.telephone }}
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
I want a user to not be able to enter twice with the same email, so it should error when the user clicks submit with a used email address.
Also, does anyone know how to get the error messages to show before the user submits rather than afterwards?
You return a newly instantiated form when the form is invalid, when you should instead return the invalid form (with the errors). Try:
def lottery_new(request):
if request.method == 'POST':
form = LotteryForm(request.POST)
if form.is_valid():
....
else:
return render(request, 'lottery/lottery.html' {'form': form})
I have some issues with validation.When i am submitting the form, the form.is_valid():
always returns false.
After modifying the form i.e. after removing all validations from the form, it still return the false(my form is not submitted)
the code of html
{% 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 %}
<div class="register_div">
<p><label for="username">Username:</label></p>
<p>{{ form.username }}</p>
</div>
<div class="register_div">
<p><label for="email">Email:</label></p>
<p>{{ form.email }}</p>
</div>
<div class="register_div">
<p><label for="password">Password:</label></p>
<p>{{ form.password }}</p>
</div>
<div class="register_div">
<p><label for="password1">Verify Password:</label></p>
<p>{{ form.password1 }}</p>
</div>
<div class="register_div">
<p><label for="birthday">Birthday:</label></p>
<p>{{ form.birthday }}</p>
</div>
<div class="register_div">
<p><label for="name">Name:</label></p>
<p>{{ form.username }}</p>
</div>
<p><input type="submit" value="submit" alt="register"/></p>
</form>
{% endblock %}
the code of forms.py
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 password1 != password:
raise forms.ValidationError("The Password did not match, please try again.")
return password
class LoginForm(forms.Form):
username = forms.CharField(label=(u'User Name'))
password = forms.CharField(label=(u'Password'), widget=forms.PasswordInput(render_value=False))
and the code of views.py
def DrinkerRegistration(request):
if request.user.is_authenticated():
return HttpResponseRedirect('/profile/')
if request.method == 'POST':
form = RegistrationForm(request.POST)
#return render_to_response('register')
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))
the model code
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
class Drinker(models.Model):
user =models.OneToOneField(User)
birthday =models.DateField()
name =models.CharField(max_length=100)
def __unicode__(self):
return self.name
#create our user object to attach to our drinker object
def create_drinker_user_callback(sender, instance, **kwargs):
drinker, new=Drinker.objects.get_or_create(user=instance)
post_save.connect(create_drinker_user_callback, User)
I can see two possible reasons:
Your template contains two usages of username: once at the top and once at the bottom. The form might fail because this does not fit into a single CharField.
It's hard to say without your model class, but maybe it contains fields you haven't explicitly excluded?
If it's not option 1, could you post your Drinker model as well?
Did you checked the errors reporting while validating?
use print form.errors and check your console for error messages
If your code changes aren't reflecting in the run environment, look at removing your *.pyc files. These sometimes can get cached.
For linux OS you can run the following from the root of your project;
find . -iname "*.pyc" -exec rm -f {} \;