This is the forms.py file:
class MyForm(forms.Form):
data = forms.CharField(required=True)
def clean(self):
#super(MyForm, self).clean()
if any(self.errors):
print self.errors
return
value = self.cleaned_data['data']
if int(value)%2 != 0:
print "invalid data"
raise forms.ValidationError(_("Please enter an even number"))
This is my view.py file:
def home(request):
if request.method == 'POST':
form = MyForm(request.POST)
if form.is_valid():
return HttpResponse("thanks")
else:
form = MyForm()
context = {
'form': form,
}
return render(request, 'todayhome.html', context)
And this is my todayhome.html file:
<form method="post" action="">{% csrf_token %}
{{form.as_p}}
{{authorformset}}
<input type="submit" value="submit" name="submit"/>
</form>
What I don't understand is :
Do I need to call
super(MyForm, self).clean()
inside 'clean' method of forms.py explicitly, or will it be called automatically once I do
self.errors?
If I remove
if any(self.errors):
print self.errors
return
from the 'clean' method of forms.py file and submit the form empty, it renders shows
'KeyError (data)'
instead of showing 'This field is required.' Why is it so ?
Do I need to call super(MyForm, self).clean() inside 'clean' method of forms.py explicitly, or will it be called automatically once I do
You need to call it explicitly. That is the method which populates the self.errors attribute, when some error is found. Using self.errors you're just accessing that attribute, not calling any method.
For your second question, it shows KeyError while accessing self.cleaned_data['data'] (in case when you call super clean method), because when a particular key is added to self.errors, it is not added to self.cleaned_data. You need to check the key existence or self.errors first, before accessing any cleaned_data key.
Related
I have a custom validator which should raise an error on the frontend for the user if the validation fails. However, I am not sure where and how to return this to the frontend. Right now it only shows in the terminal/Django error site on dev server:
The view:
def raise_poller(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = PollersForm(request.POST)
# check whether it's valid:
if form.is_valid():
# Make Validation of Choices Fields
poller_choice_one = form.cleaned_data['poller_choice_one']
poller_choice_two = form.cleaned_data['poller_choice_two']
if ' ' in poller_choice_one or ' ' in poller_choice_two:
raise ValidationError('Limit your choice to one word', code='invalid')
return poller_choice_two # !! IDE says this is unreachable
else:
# process the remaining data in form.cleaned_data as required
poller_nature = form.cleaned_data['poller_nature']
poller_text = form.cleaned_data['poller_text']
poller_categories = form.cleaned_data['poller_categories']
# Get the user
created_by = request.user
# Save the poller to the database
p = Pollers(poller_nature=poller_nature,
poller_text=poller_text,
poller_choice_one=poller_choice_one,
poller_choice_two=poller_choice_two,
created_by=created_by)
p.save()
p.poller_categories.set(poller_categories)
# redirect to a new URL:
return HttpResponseRedirect('/')
# if a GET (or any other method) we'll create a blank form
else:
form = PollersForm()
return render(request, 'pollinator/raise_poller.html', {'form': form})
template
{% block content %}
<div class="poll-container">
<form action="/raisepoller/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
</div>
{% endblock %}
Also where to best structure/implement custom validators? After the if form.is_valid part?
To show the error in the form, you have to raise the ValidationError in the form (instead of the view), because form expects and handles when ValidationError is eventually raised. Read the docs about forms validation here
class PollersForm(forms.Form):
def clean(self):
data = self.cleaned_data
poller_choice_one = data['poller_choice_one']
poller_choice_two = data['poller_choice_two']
if ' ' in poller_choice_one or ' ' in poller_choice_two:
# raising ValidationError here in clean does the trick
raise ValidationError('Limit your choice to one word', code='invalid')
return data
sidenote: if the only thing you are doing with the form is creating a model instance, you should consider using ModelForm instead, which is designed exactly for this purpose. It will save you writing a lot of code.
MODELS.PY
class Campaign(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
campaign_image = models.ImageField(default="profilepic.jpg",upload_to="campaign_pictures")
FORMS.PY
class RaiseFundsFrom3(forms.ModelForm):
class Meta:
model = Campaign
fields = ['campaign_image']
VIEWS.PY
#login_required
def raise_funds_medical_3(request):
if request.method == 'POST':
form = RaiseFundsFrom3(request.POST, request.FILES or None, instance=request.user)
if form.is_valid():
check = form.save(commit=False)
check.save()
return HttpResponse('form worked')
else:
form = RaiseFundsFrom3()
return render(request,'funds/raise_funds_medical_3.html',{'form':form})
URLS.PY
path('raise/medical/photo', views.raise_funds_medical_3, name="raise_funds_medical_3"),
raise_funds_medical_3.html
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<div class="form-group pt-2">
<small>Photo formats must be PNG / JPG / JPEG</small>
<input type="file" name="campaign_image" />
</div>
<button class="btn btn-lg button_bfg_blue" type="submit"> <small><b> NEXT </b></small> </button>
</form>
on form submit, i do not get any error, but image is not uploaded to the required folder.
however, in the raise_funds_medical_3 function within views.py, if i remove instance=request.user, the image gets uploaded but i get following error : NOT NULL constraint failed: funds_campaign.user_id
Your form is a ModelForm for a Campaign, so its instance needs to be a Campaign. Don't assign request.user as its instance!
Now, your form isn't including the user field which is required to save a Campaign, so you should assign that yourself in the view before saving to the database:
campaign = form.save(commit=False) # this gives your the form's instance
campaign.user = request.user # this assigns the user
campaign.save() # this commits to the database
Also you should handle the case where the form isn't valid. This is quite simple, just un-indent the last return in your view function, so that return render(...) is also called in case the form isn't valid.
Finally, instead of returning a response when the form is valid, it's good practice to redirect to another view. This way, when the user refreshes the page, the form isn't submitted again. Your final code should look like this:
#login_required
def raise_funds_medical_3(request):
if request.method == 'POST':
form = RaiseFundsFrom3(request.POST, request.FILES or None)
if form.is_valid():
check = form.save(commit=False)
check.user = request.user
check.save()
return redirect(<url_pattern>)
else:
form = RaiseFundsFrom3()
return render(request,'funds/raise_funds_medical_3.html',{'form':form})
Supplementary answer to dirkgroten's one
I have come to completely hate the conventional structuring of a Django Function-based View. They can be re-factored by inverting the validity test and adding one line so that one and only one instantiation of a form is present. The result is IMO far easier to read, and easily generalizes for a view displaying two or more forms.
def raise_funds_medical_3(request):
args = [request.POST, request.FILES or None] if request.method == "POST" else []
form = RaiseFundsFrom3(*args)
if request.method != "POST" or not form.is_valid():
# unbound form or form not valid
return render(request,'funds/raise_funds_medical_3.html',{'form':form})
# form is valid so do the processing and redirect
check = form.save(commit=False)
check.user = request.user
check.save()
return redirect(<url_pattern>)
If you want to process >1 form, the test becomes
if request.method != "POST" or any(
[ not form.is_valid(), not form2.is_valid(), ...]):
which forces evaluation of .is_valid() for all forms, even if the first was not valid, so that all the error messages are shown to the user.
In a complex business application, the processing of a successful form submission may be quite a few more lines of code than this simple example. Having it at the end, not indented, isolated from all the boilerplate save the return redirect(...), makes things much easier!
I am using a Django form wizard to enter data into a form page, and then display it in a confirmation page. However, when I try to call self.get_cleaned_data_for_step(step_name), I get a "'MyForm' object has no attribute 'cleaned_data'." I know this can happen if the form fails to validate, so I overrode the is_valid method in my form class to always return True, just as a test, but I still get this error. My relevant code is below:
forms.py
...
class MealForm(forms.Form):
modifications = forms.CharField()
def __init__(self, *args, **kwargs):
menu_items = kwargs.pop('menu_items')
super(MealForm, self).__init__(*args, **kwargs)
for item in menu_items:
self.fields[str(item.name)] = forms.IntegerField(widget=forms.NumberInput(attrs={'value': 0}))
def is_valid(self):
return True
urls.py
...
url(r'^(?P<url>[-\w]+)/meal/$',
login_required(views.MealFormWizard.as_view(views.MealFormWizard.FORMS)), name="meal"),
views.py
...
class MealFormWizard(SessionWizardView):
FORMS = [('meal_form', MealForm),
('meal_form_confirmation', MealFormConfirmation)]
TEMPLATES = {'meal_form': 'restaurant/createMeal.html',
'meal_form_confirmation': 'restaurant/confirmation.html'}
def get_form_kwargs(self, step=None):
kwargs = {}
url = self.kwargs['url']
restaurant = Restaurant.objects.get(url=url)
menu_items = MenuItem.objects.filter(restaurant=restaurant)
if step == 'meal_form':
kwargs['menu_items'] = menu_items
return kwargs
def get_context_data(self, form, **kwargs):
context = super(MealFormWizard, self).get_context_data(form=form, **kwargs)
if self.steps.current == 'meal_form':
context.update({...objects/vars...})
if self.steps.current == 'meal_form_confirmation':
cd = self.get_cleaned_data_for_step('meal_form') **This is where my error occurs**
createMeal.html
...
<form action="" method="post">
{% csrf_token %}
{{ wizard.management_form }}
{{ wizard.form }}
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.next }}">Submit Meal</button>
</form>
Upon submitting the form in createMeal.html I should be able to access the cleaned data for the previous step in the get_context_data method of my MealFormWizard class in views.py via a call to self.get_cleaned_data_for_step('meal_form'). However, this is not the case, but I am not sure where I went wrong.
Overriding is_valid like that won't work - if you follow the code you will see that the form's cleaned_data attribute is set by the normal is_valid method.
The docs say that if the form is invalid, then get_cleaned_data_for_step will return None, you need to write your code so it can handle this.
In case this is helpful to anyone. My problem was that in my createMeal.html, my button was simply taking the wizard to the next step, bypassing any validation. The proper solution is to make a simple submit button to submit the form, at which point the wizard will then validate the form, and if it is valid, it will move on to the next step.
I am trying to assign a session variable based on a model or database table on my Django site. In other words, on the first use, I want the user to select a county from a dropdown list and write the name of that county or pk to a session variable. From therek, that data gets pulled into context_processor before hitting the template. Right now, the error I'm getting is
(1062, "Duplicate entry '1' for key 'county_id'")
I'm not sure if it is trying to write multiple entries into the database, but I don't really want anything databased as you would other data. I just want a session variable stored. I am sure my problem is my view, but I can't seem to get it right. In case you couldn't tell, I'm pretty new at this.
I have a model.py defined that accesses another table of counties.
class NonUserCounty(models.Mdel):
county = models.ForeignKey(County)
def __unicode__(self):
return self.county
I have defined a form.py
class NonUserCountyForm(forms.ModelForm):
class Meta:
model = NonUserCounty
And a views.py
def Welcome(request):
if request.method == 'POST':
form = NonUserCountyForm(request.POST)
if form.is_valid():
mycounty = form.cleaned_data['county']
request.session['mycounty'] = mycounty
form.save()
return HttpResponseRedirect('/')
else:
form = NonUserCountyForm()
context = {'form': form}
return render_to_response('welcome.html', context, context_instance=RequestContext(request))
A Context processor
def mynews(request):
now = datetime.now()
if not request.user.is_authenticated():
if not "mycounty" in request.GET:
MyNews = News
county = County
else:
return HttpResponseRedirect('/welcome/')
else:
user = request.user.get_profile()
county = user.county.all()
MyNews = News
And my template....
<form action="" method="post">
{% csrf_token %}
{{ form }}
<p><input type="submit" alt="register" value="Sign Up" /></p>
Why are you calling form.save() if you don't want to save anything in the database? Just remove that line and it should work.
I have created a Class view in views.py of the django application.
class HelloTemplate(TemplateView):
template_name = "index.html"
def get_context_data(self, **kwargs):
context = super(HelloTemplate, self).get_context_data(**kwargs)
return context
Now I have a form defined in the html page:
<form method="get">
<input type="text" name="q">
<input type="text" name="q1">
<input type="submit" value="Search">
</form>
As you can see, I am submitting the form on the same page.
Now I want to get the form submitted values in my HelloTemplate class. I don't want to create another class or methods outside the existing class.
Also, I would like to send an error message to the html form if data is not validated in the django.
I don't know how to do this, please help me out.
You need to define get (because your form defined with get method <form method="get">) method in view class:
class HelloTemplate(TemplateView):
template_name = "index.html"
def get_context_data(self, **kwargs):
context = super(HelloTemplate, self).get_context_data(**kwargs)
return context
def get(self, request, *args, **kwargs):
q = request.GET.get('q')
error = ''
if not q:
error = "error message"
return render(request, self.template_name, {'error': error})
More information in django docs here Introduction to Class-based views
There's only one value, and it's in request.GET['q'].
Quick response, I can show you what I did a while ago for a review form (for people to create a new review, one of my models):
def review_form_view(request):
c = {}
c.update(csrf(request))
a = Review()
if request.method == 'POST':
review_form = Review_Form(request.POST, instance=a)
if review_form.is_valid():
a = review_form.save()
return HttpResponseRedirect('../dest_form_complete')
pass
else:
review_form = Review_Form(instance=a)
return render_to_response('../review_form.html', {
'review_form': review_form,
}, context_instance=RequestContext(request))
If you have a user model, comment model, etc. you can probably use something similar to this. Very (very) roughly put, the request is the input that the user fills out in the form, 'POST' is the method called that lets the server know you are adding entries to your database, and is_valid() validates the data according to your models.py parameters (can name be NULL? Is age an integer? etc).
Take a look at https://docs.djangoproject.com/en/dev/topics/forms/ as well for more examples and explanation.