Validate/clean a FileField on a non-model form in Django? - django

I'm ultimately trying to validate a FileField by extension type. But I'm having trouble even getting the clean method for this field to pickup the POSTed value.
from django.forms.forms import Form
from django.forms.fields import FileField
from django.forms.util import ValidationError
class TestForm(Form):
file = FileField(required=False)
def clean_file(self):
value = self.cleaned_data["file"]
print "clean_file value: %s" % value
return None
#localhost
def test_forms(request):
form = TestForm()
if request.method == "POST":
form = TestForm(request.POST)
if form.is_valid():
print "form is valid"
return render_to_response("test/form.html", RequestContext(request, locals()))
When I run the code, I'm getting the following output:
clean_file value: None
form is valid
In other words, the clean_file method is not able to get the file data. Likewise, if it returns None, the form is still valid.
Here is my form html:
<form enctype="multipart/form-data" method="post" action="#">
<input type="file" id="id_file" name="file">
<input type="submit" value="Save">
</form>
I have seen a couple snippets with solutions for this problem, but I cannot get them to work with a non-model form. They both declare a custom field type. When I do that, I get the same problem; calling super() returns a None object.

You're not passing request.FILES into the form when you instantiate it in the post.
form = TestForm(request.POST, request.FILES)
See the documentation.
Also note that you're instantiating the form twice on POST, which is unnecessary. Move the first one into an else clause at the end of the function (at the same level as if request.method == 'POST').

Related

Bind dynamic choices to ModelForm in Django

I'm trying to bind a dynamic list of choices to a ModelForm. The form is rendered correctly. However, when using the form with a POST Request, I get an empty form back. My goal is to save that form into the database (form.save()). Any help would be much appreciated.
Model
I'm using a multiple choice select field ( https://github.com/goinnn/django-multiselectfield )
from django.db import models
from multiselectfield import MultiSelectField
class VizInfoModel(models.Model):
tog = MultiSelectField()
vis = MultiSelectField()
Forms
class VizInfoForm(forms.ModelForm):
class Meta:
model = VizInfoModel
fields = '__all__'
def __init__(self,choice,*args,**kwargs):
super(VizInfoForm, self).__init__(*args,**kwargs)
self.fields['tog'].choices = choice
self.fields['vis'].choices = choice
View
Choices are passed from the view when instantiating the form.
def viz_details(request):
options = []
headers = request.session['headers']
for header in headers :
options.append((header, header))
if request.method == 'POST':
form = VizInfoForm(options, request.POST)
#doesnt' get into the if statement since form is empty!
#choices are not bounded to the model although the form is perfectly rendered
if form.is_valid():
form.save()
return HttpResponseRedirect('/upload')
else:
#this works just fine
form = VizInfoForm(options)
return render(request, 'uploads/details.html', {'form': form})
Template
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<p>Choose variables to toggle between</p>
{{ form.tog }}
<br></br>
<p>Choose variable to be visualized</p>
{{ form.vis }}
<br></br>
<button type="submit">Submit</button>
</form>
You're saying Django doesn't get into your if request.method == 'POST' block.
This tells us that you're not sending your request through the POST method. Your template probably has an error in it, maybe you haven't specified the method on your form, or you made your button to just be a link instead of a submit ?
Show your template so we can say more, unless this was enough to solve your question !

Using extra and max_num in a Django formset

I have a formset that has no model associated with it and I want to be able to add a form to the formset once all existing forms are valid, so reading the docs, I found: "If the value of max_num is greater than the number of existing objects, up to extra additional blank forms will be added to the formset, so long as the total number of forms does not exceed max_num."(https://docs.djangoproject.com/en/dev/topics/forms/formsets/#limiting-the-maximum-number-of-forms):
So I did this:
FormSet = formset_factory(SomeForm, extra=2, max_num=10)
if request.method == 'POST':
formset = FormSet(data=request.POST)
else:
formset = FormSet()
and this:
<form action="" method="POST">
{{ formset }}
<input type="submit" value="Next" />
</form>
expecting to see 2 empty forms, where I would get extra forms if I filled out one (or 2) forms and pressed "Next". However, only 2 forms are ever shown in the template even if I have 1 or 2 valid forms.
How is this supposed to work? Am I misinterpreting the docs? Is my code wrong?
I found a partial answer to my question: I got it to work, but I find the solution not very Django-like. I would expect this stuff to happen automatically, without the cruft below.
Anyway, I changed my view thus:
if request.method == 'POST':
formset = FormSet(data=request.POST)
if formset.is_valid():
clean_data = formset.cleaned_data
if not any(not(len(f)) for f in clean_data):
formset = FormSet(initial=clean_data)
else:
formset = FormSet()
So I re-instantiated the formset using cleaned_data from the POST data and added some stuff to prevent an extra form popping up if you press "Next" while there is still an empty form.
It works, but I really don't think this should be the way to do this.

ModelForm with ImageFields, not clearing properly if validation error

First the code.
The ModelForm (im1 and im2 are models.ImageField):
class TestForm(forms.ModelForm):
checkme = forms.BooleanField(required=True)
class Meta:
model = UserProfile
fields = ('im1', 'im2')
The view:
def test(request):
profile = request.user.get_profile()
form = TestForm(instance=profile)
if request.method == "POST":
form = TestForm(request.POST, request.FILES, instance=profile)
if form.is_valid():
form.save()
return render(request, 'test.html', {'form':form})
The template:
<html>
<head>
<title>Test</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="submit" />
</form>
</body>
</html>
The problems:
If im1 contains a valid image, and I check the clear checkbox next to it but don't check checkme and submit, the form comes back with an error saying that checkme is required. Although the form returns with the error, it appears as if im1 has been cleared. In reality it has not because if I reload the form im1 shows back up with its file and clear checkbox.
My question is how can I fix this? Is it something I am doing or is this something to do with django?
Django is acting exactly as it should.
If the request is a POST request, then your form is bound to the data from request.POST and request.FILES. instance=profile is simply telling the form what particular object to save to if all validation passes. Even if your form isn't valid, it's still bound to the data with the cleared image, and that's what you're passing to render().
Firstly, you shouldn't be creating the first bound form if the request method is POST:
def test(request):
profile = request.user.get_profile()
if request.method == "POST":
form = TestForm(request.POST, request.FILES, instance=profile)
if form.is_valid():
form.save()
else:
form = TestForm(instance=profile)
return render(request, 'test.html', {'form':form})
Secondly, why do you want your user to do the same exact action twice if they did indeed want to delete an image but simply missed another checkbox?
If you really need Django to act this way, I would do one of two things. Either create a bound form from an instance of UserProfile and pass both the non-valid form and the newly created form to the template and use the non-valid form for displaying the errors and the other one for displaying the rest of the form:
def test(request):
profile = request.user.get_profile()
if request.method == "POST":
errors_form = TestForm(request.POST, request.FILES, instance=profile)
if errors_form.is_valid():
errors_form.save()
form = errors_form
else:
form = TestForm(instance=profile)
return render(request, 'test.html', {'form':form, 'errors_form': errors_form})
else:
form = TestForm(instance=profile)
return render(request, 'test.html', {'form':form})
OR I'd do the same thing but save the errors from the non-valid form to the newly created form so you don't end up with renders() all over the place:
def test(request):
profile = request.user.get_profile()
if request.method == "POST":
errors_form = TestForm(request.POST, request.FILES, instance=profile)
if errors_form.is_valid():
errors_form.save()
form = errors_form
else:
form = TestForm(instance=profile)
#this is left up to you to implement, but you'd do something like
#form.errors = errors_form.errors
#and also iterate through every form attribute.errors and assign it to
#errors_form attribute.errors etc...
else:
form = TestForm(instance=profile)
return render(request, 'test.html', {'form':form})
Both aren't very elegant solutions and I'm not positive the second one will even work as expected without some more hacks since I'm not completely familiar with the Django Forms implementation.
I don't see that doing this is worth it. As I stated before, you're just creating more work for your user...

Parsing error message to Django template?

I am a newbie in Django. I am writing a sample application that has a form, submit that form then saving the data into the database. My form need to be validated before allowing to save data. So my question is how can I pass the error messages (that generated by validation) to the view? Any suggestion are welcome. Thanks!
Are you using a Form instance? Then you can render the form in the template and the error messages with automagically show up. For instance:
# views.py
def my_view(request, *args, **kwargs):
if request.method == 'POST':
form = MyForm(request.POST.copy())
if form.is_valid():
# Save to db etc.
elif request.methof == 'GET':
form = MyForm()
return render_to_response(..., {'form' : form})
And in the template:
{{ form.as_p }}
You will note that if the form is not vald (is_valid() returns False) then the view will proceed to return the form (with errors) to the template. The errors then get rendered in the template when form.as_p is called.
** Update **
As #Daniel said:
Even if you're not using form.as_p, you can get the errors for the whole form with form.errors and for each field with form.fieldname.errors.

Pass an initial value to a Django form field

Django newbie question....
I'm trying to write a search form and maintain the state of the input box between the search request and the search results.
Here's my form:
class SearchForm(forms.Form):
q = forms.CharField(label='Search: ', max_length=50)
And here's my views code:
def search(request, q=""):
if (q != ""):
q = q.strip()
form = SearchForm(initial=q)
#get results here...
return render_to_response('things/search_results.html',
{'things': things, 'form': form, 'query': q})
elif (request.method == 'POST'): # If the form has been submitted
form = SearchForm(request.POST)
if form.is_valid():
q = form.cleaned_data['q']
# Process the data in form.cleaned_data
return HttpResponseRedirect('/things/search/%s/' % q) # Redirect after POST
else:
form = SearchForm()
return render_to_response('things/search.html', {
'form': form,
})
else:
form = SearchForm()
return render_to_response('things/search.html', {
'form': form,
})
But this gives me the error:
Caught an exception while rendering: 'unicode' object has no attribute 'get'
How can I pass the initial value? Various things I've tried seem to interfere with the request.POST parameter.
Several things are not good here...
1) The recommended thing after a POST is to redirect. This avoids the infamous popup saying that you are resubmitting the form when using the back button.
2) You don't need to say if request.method == 'POST', just if request.POST. That makes your code easier to read.
3) The view generally looks something like:
def myview(request):
# Some set up operations
if request.POST:
form=MyForm(request.POST)
if form.is_valid():
# some other operations and model save if any
# redirect to results page
form=MyForm()
#render your form template
That is not to say that there can't be much simpler and much more complicated views. But that is the gist of a view: if request is post process the form and redirect; if request is get render the form.
I don't know why you are getting an unicode error. I can only think that it is related to one of your models that you don't provide.
The error, as spookylukey mentions is in his comment, most likely is caused by you submitting a string instead of a dict to the initial parameter.
I really recommend the django documentation, in particular the tutorial., but there is also the very nice Django Book.
All that said, I think you want something like:
def search(request, q=None):
if request.POST:
form = SearchForm(request.POST)
if form.is_valid():
q = form.cleaned_data['q']
url=reverse('search_results', args=(q,))
return HttpResponseRedirect(url)
if q is None:
form = SearchForm()
else:
form = SearchForm(initial={'q': q})
return render_to_response('things/search.html', {
'form': form,
})
Notice that the parameter to initial is a dict of the field values of your form.
Hope that helps.
Django forms are not particularly helpful for your use case. Also, for a search page, it's much better to use a GET form and maintain state in the URL. The following code is much shorter, simpler and conforms far better to HTTP standards:
def search(request):
q = request.GET.get('q','').strip()
results = get_some_results(q)
render_to_response("things/search.html", {'q': q, 'results': results})
The template:
<form method="GET" action=".">
<p><input type="text" value="{{ q }}" /> <input type="submit" value="Search" /></p>
{% if q %}
{% if results %}
Your results...
{% else %}
No results
{% endif %}
{% endif %}
</form>