Django form.save() not taking updated form.cleaned_data - django

I'm lost here and can't figure out what I'm missing, which is probably something stupid, but I need another set of eyes because as far as I can tell this should be working.
What I'm trying to do is allow my users to enter phone numbers in ways they are used to seeing, but then take that input and get a validated international phone number from Twilio and save it. By definition that means it will be in the following format - which is the format I need to have it in the database so that it interacts well with another part of the application:
+17085551212
I've debugged to the point there I know the values are coming in correctly, everything works right if I get an invalid number, etc. For some reason, the updated value is not being passed back to the form when I set form.cleaned_data['office_phone'] prior to the form.save(). So I am getting the original number (708) 555-1212 in the database.
forms.py
class ProfileForm(forms.ModelForm):
office_phone = forms.CharField(max_length=20, label="Office Phone")
views.py
if form.is_valid():
print (form.cleaned_data['office_phone'])
pn = form.cleaned_data['office_phone'].replace(" ","")
try:
response = validator.phone_numbers.get(str(pn))
form.cleaned_data['office_phone'] = str(response.phone_number)
print form.cleaned_data
form.save()
success_message = "Your changes have been saved"
except:
error_message = "The contact phone number you entered is invalid."
console.output
(708) 555-1212
+17085551212
+17085551212
{'office_phone': '+17085551212'}
<tr><th><label for="id_office_phone">Office Phone:</label></th>
<td><input id="id_office_phone" maxlength="20" name="office_phone" type="text" value="(708) 555-1212" /></td></tr>
What am I missing here?

Made an edit: I realise that instead of overriding save, we should instead clean/validate the phone number by using custom validation:
class ProfileForm(forms.ModelForm):
office_phone = forms.CharField(max_length=20, label="Office Phone")
def clean_office_phone(self):
value = self.cleaned_data.get("office_phone")
try:
value = value.replace(" ", "")
response = validator.phone_numbers.get(str(value))
except:
raise ValidationError("The contact phone number you entered is invalid")
return str(response.phone_number)
views.py:
if form.is_valid():
form.save()
success_message = "Your changes have been saved"

Related

forms.ValidationError bug?

this is my third day in django and I'm working on my validation form and i came across this weird bug where my validation form didn't do anything so here's the code
class RegisterForm(forms.ModelForm):
email = forms.EmailField(label="E-Mail", error_messages={'required': 'Please enter your name'})
class Meta:
model = LowUser
fields =['fullname', 'email', 'password', 'bio']
widgets = {
'password' : forms.PasswordInput()
}
def clean_fullname(self):
data = self.cleaned_data.get("fullname")
if 'ab' in data:
raise forms.ValidationError('invalid')
else:
return data
if i input "ac" to the fullname it works perfectly fine it adds the input to the database. But if i input "ab" it didn't do anything it doesn't give me any errors nor add the input to my database. And I'm pretty sure my forms.ValidationError is bugging because if i change my raise forms.ValidationError('invalid') to raise NameError('Test') like this
def clean_fullname(self):
data = self.cleaned_data.get("fullname")
if 'ab' in data:
raise NameError('Test')
else:
return data
and i input "ab". It works completely fine and it gave me this page
and I'm using django 2.1.5 if you're wondering i would appreciate any help
thank you in advance
If i input "ac" to the fullname it works perfectly fine it adds the input to the database. But if i input "ab" it didn't do anything it doesn't give me any errors nor add the input to my database.
That is expected behavior, ValidationErrors are used to collect all errors.
The idea is that you raise ValidationErrors. These are all collected, and when one such error is present, form.is_valid() will return False, and the form.errors will contain a dictionary-like object with all the errors. The reason this is done is to collect problems with all fields in one single pass, such that it does not only report the first error of the form data.
Imagine that you have five fields with mistakes, and the form only reports problems with the first field. Then it takes five rounds before all fields are validated. By collecting all errors, it can show multiple ones. You can even return multiple errors on the same field.
For more information, see the raising ValidationError` section of the Django documentation.
Thanks to Willem i realized that the problem was in my views.py.
def registerForm(request):
regisform = RegisterForm()
cntxt = {'mainregisform': regisform, 'tst': ''}
if request.method == 'POST':
regisform = RegisterForm(request.POST)
if regisform.is_valid():
regisform.save()
return render(request, 'userregister.html', cntxt)
i thought that the ValidationError wasn't giving me any errors because usually there's an error message on top of my input box, but it actually did gave me an error. the problem was i define the mainregisform before the regisform got re-rendered therefore i never got error message

Generate short temporary link to a form

So, I have a form that logged in user can fill, and when that form is submitted, I'd like it to generate a temporary link to another form that non-logged in users could access and fill, that would last 24 hours. That form would then send data to the database. That link would be valid only several times. that number is determined in the first form, by the logged in user.
I would like that URL to be as short as possible and easy to write down manually on a browser, but I don't know the best way to do so, or even how to do so.
The first form is completed, and only logged in user can fill it, but I do not know how to generate that temporary link.
Please keep in mind that I am fairly new to Django.
Views.py
class GenFormPageView(LoginRequiredMixin, TemplateView):
login_url = '/'
def get(selfself, request, **kwargs):
if request.method == 'POST':
formation_form = genForm(data=request.POST)
if formation_form.is_valid():
cdata = formation_form.cleaned_data.get
else:
print('Invalid')
else:
formation_form = genForm()
return render(request, 'genform.html', {'formation_form': formation_form})
Forms.py
class genForm(forms.ModelForm):
liste_formation = []
formations_info = {}
for formation in Formation.objects.all():
if formation.libelle in formations_info:
formations_info[formation.libelle].append(formation.formateur, formation.dateDebut, formation.dateFin,
formation.nbJours, formation.cours)
else:
info = {'formateur':formation.formateur.get_full_name(), 'dateDebut':formation.get_debut(), 'dateFin':formation.get_fin(), 'nbJours':formation.nbJours, 'cours':formation.cours.get_titre()}
formations_info[formation.libelle] = info
liste_formation.append(formations_info)
formations = [str(formation) for formation in Formation.objects.all()]
formation_select = forms.ChoiceField(label='Formation', choices=([(formation, formation) for formation in formations]), required = True)
formateur = forms.CharField(label='Formateur')
cours = forms.CharField(label='Cours')
nombre_eleves = forms.IntegerField(label='Nombre d\'élèves', min_value=1)
formations = json.dumps(formations_info)
class Meta:
model = Formation
fields = ('formation_select', 'formateur', 'dateDebut', 'dateFin', 'nbJours', 'cours', 'nombre_eleves')
urls.py
url(r'^genform/$', views.GenFormPageView.as_view()),
Here is the field tht is going to determine the number of time the new form can be submitted
When I click the button 'Generer', it creates a link to a form. that form can only be submitted a fixed number of time, and the link can only lead to that form if clicked within 24 hours from the generated time.
What I am trying right now, is creating a field in my database containing the time when the form is generated, and then I will check if the time is valid, and display the form only if it is.
That my idea right now, but I still don't konw how to do it.
So, to answer my question, I finally decided to use a field in my database containing the date of the creation of the form, then I would just check that today's date is within 24 hours from the date in my database.

Django Form Validation don't keep password with ValidationError

I have written clean field based validation. To check I enter unmatching passwords this wipe off passwords from both the fields. This can annoy user. I want this like "If error let the user change a bit..don't retype whole password". However username validation keeps the username in the field
after error.Why this is happening?
class signupform(ModelForm):
prmqpass=forms.CharField(widget=forms.PasswordInput())
prmqpass1=forms.CharField(widget=forms.PasswordInput())
class Meta:
model=prmqsignup
exclude=['prmqactivated']
def clean_prmqpass(self):
cd=self.cleaned_data
pwd=cd.get("prmqpass")
if len(str(pwd))<6:
raise ValidationError("Password must be at least of six characters")
if not passstrength(pwd):
raise ValidationError("For stronger security password must contain at least one uppercase,lowercase,number and special character")
return pwd
def clean_prmquname(self):
cd=self.cleaned_data
usrname=cd.get("prmquname")
if len(usrname)<5:
raise ValidationError("Username too short choose more than four characters")
if prmqsignup.objects.exclude(pk=self.instance.pk).filter(prmquname=usrname).exists():
raise ValidationError('Username "%s" is already in use.' % usrname)
return usrname
def clean(self):
cd=self.cleaned_data
pwd=cd.get("prmqpass")
pwd1=cd.get("prmqpass1")
if not pwd==pwd1:
raise ValidationError("Passwords don't match.")
return cd
Why username value(staying in form) and password value(flashing away) after error.See validation logic is same.
From the documentation:
Password input: <input type='password' ...>
Takes one optional argument:
render_value: Determines whether the widget will have a value filled in when the form is re-displayed after a validation error (default is False).
So you need to initialise your inputs like so:
prmqpass = forms.CharField(widget=forms.PasswordInput(render_value=True))

Django custom validation of multiple fields

for which I want to validate a number of fields in a custom clean method.
I have this so far:
class ProjectInfoForm(forms.Form):
module = forms.ModelChoiceField(
queryset=Module.objects.all(),
)
piece = forms.CharField(
widget=forms.Select(),
required=False,
)
span = forms.IntegerField(
max_value=100,
initial=48
)
max_span = forms.IntegerField(
max_value=100,
initial=0
)
def clean(self):
span = self.cleaned_data['span']
max_span = self.cleaned_data['max_span']
piece = self.cleaned_data.['piece']
# validate piece
try:
Piece.objects.get(pk=m)
except Piece.DoesNotExist:
raise forms.ValidationError(
'Illegal Piece selected!'
)
self._errors["piece"] = "Please enter a valid model"
# validate spans
if span > max_span:
raise forms.ValidationError(
'Span must be less than or equal to Maximum Span'
)
self._errors["span"] = "Please enter a valid span"
return self.cleaned_data
However, this only gives me one of the messages if both clauses invalidate. How can I get all the invalid messages. Also I do not get the field-specific messages - how do I include a message to be displayed for the specific field?
Any help much appreciated.
Store the errors and don't raise them until the end of the method:
def clean(self):
span = self.cleaned_data['span']
max_span = self.cleaned_data['max_span']
piece = self.cleaned_data.['piece']
error_messages = []
# validate piece
try:
Piece.objects.get(pk=m)
except Piece.DoesNotExist:
error_messages.append('Illegal Piece selected')
self._errors["piece"] = "Please enter a valid model"
# validate spans
if span > max_span:
error_messages.append('Span must be less than or equal to Maximum Span')
self._errors["span"] = "Please enter a valid span"
if len(error_messages):
raise forms.ValidationError(' & '.join(error_messages))
return self.cleaned_data
You should write a custom clean_FIELDNAME method in this case. That way, field centric validation errors can later be displayed as such when using {{form.errors}} in your template. The clean method o.t.h. is for validating logic that spans more than one field. Take a look through the link I posted above, everything you need to know about validating django forms is in there.
It happens because you are using raise.
Try replace it by these two line in your code:
del self.cleaned_data['piece']
and
del self.cleaned_data['span']
It appears this has changed in later versions of Django (this seems to work in 2.1 and later):
from django import forms
class ContactForm(forms.Form):
# Everything as before.
...
def clean(self):
cleaned_data = super().clean()
cc_myself = cleaned_data.get("cc_myself")
subject = cleaned_data.get("subject")
if cc_myself and subject and "help" not in subject:
msg = "Must put 'help' in subject when cc'ing yourself."
self.add_error('cc_myself', msg)
self.add_error('subject', msg)
https://docs.djangoproject.com/en/dev/ref/forms/validation/#raising-multiple-errors has more details.

submitting form results into db - django

i created a form to save a post into db for my blog project. I've designed index page. now i am tryin to create a form to create new posts. before that i was using ' manage.py shell'
here is my view :
def addpost(request):
form = addForm()
if request.method=="POST":
titleform = request.POST['title']
bodyform = request.POST['body']
checkform = request.POST['isdraft']
if form.is_valid():
n = Post(title = titleform, body = bodyform, isdraft=checkform)
n.save()
return HttpResponseRedirect('/admin/')
else:
pass
return render(request,'userside/add.html',{'form':form,})
my model.py:
class Post(models.Model):
title = models.CharField(max_length = 100)
body = models.TextField()
slug = AutoSlugField(populate_from='title',unique=True)
posted = models.DateField(auto_now_add=True)
isdraft = models.BooleanField()
def __unicode__(self):
return self.title
#permalink
def get_absolute_url(self):
return ('view_blog_post',None, {'postslug':self.slug})
class addForm(forms.Form):
title = forms.CharField(max_length=100)
body = forms.CharField(widget=forms.Textarea)
isdraft = forms.BooleanField()
if i submit form as 'isdraft' field is False(unchecked) ; it gives error like:
MultiValueDictKeyError at /admin/addpost/
"Key 'isdraft' not found in "
and if i submit the form as 'isdraft' field is True(checked) ; it gives nothing. just refreshing form. no adding data into db.
i am doing sth wrong..
thank you
edit : Dmitry Beransky's answer worked for checkbox error. but it still doesnt add any data into db. just refreshes the form.
The whole point of using a form is that it takes care of validation and cleaning, that is converting values to the proper data types. That's why you should be accessing form.cleaned_data rather than reques.POST, and you should be doing it inside the if form.is_valid() check.
Edit
I've just noticed that you're never passing request.POST to the form. So form.is_valid() will never be true.
Please go back and read the documentation about using a form in a view.
If a checkbox is not checked in your HTML form, it's name/value is not going to be included in the data that the browser sends to your server. Which meanst that the request.POST dictionary is not going to contain an entry for 'isdraft' which in turn will cause a key error when you try to read the isdraft value. A solution is to change the way you read the value from the posted data to:
checkform = request.POST.get('isdraft', False)
rather than throw an error if isdraft isn't found in the dictionary, this will set checkform to False (the default value in case of a missing key)
Maybe your form does not validate at all. Have you checked if your code even reaches those lines after the if form.is_valid() statement ? If they do, what you've done there is right and should create the db row for your new entry, though you could have used
Post.objects.create(....) , and that would have taken away the need for calling the method save().
Some points though:
instead of checking for request.POST , check for request.method == 'POST' , cause there might be a post which has an empty POST dict ( in case no arguments have been submitted ), in that case request.POST fails to provide the right check .
see the docs for more info : request.POST
instead of using request.POST['var_name'] , use request.POST.get('var_name', 'default_value') , cause doing this like request.POST['var_name'] might result in some exceptions ( in case for example the argument is not provided , like what happened for your checkform variable )
Try accessing those variables through form.cleaned_data
and finally , you don't need the else statement in the end , just use the indentation :)