A user is able submit a POST request. Either by uploading a file or by using a sample on the server. Posting the file works perfect but I have trouble implementing the second part. This is my model:
class UploadFileForm(forms.Form):
file = forms.FileField()
from_server = forms.BooleanField(required=False,initial=False)
My template. I thought it best to use two forms. But I fail to check on the content/type/key of the incoming data in my view.
<div class="fieldWrapper">
<form action="flot" enctype="multipart/form-data" method="post">{% csrf_token %}
<label for="id_file">File:</label>
{{ form.file }}
<input id="upload" type="submit" value="Upload">
</form>
</div>
<div class="fieldWrapper">
<form action="flot" method="post">{% csrf_token %}
<label for="id_from_server">Gebuik een sample file op de server</label>
{{ form.from_server }}
<input id="server" type="submit" value="Go" name="Go">
<!--<input id="upload" type="submit" value="Go">-->
</form>
</div>
Here is my view. I don't know exactly how to check the incoming data on from which form they where submitted. See request.POST.getlist('Go')
for my final attempt. Test-wise I will return a Http404.
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
if 'Go' in request.POST.getlist('Go'):
raise Http404
else:
err, line_z1, line_z2 = handle_uploaded_file(request.FILES['file'])
if err == None:
return render_to_response('flot.html',
{
'form': form,
'line1':simplejson.dumps(line_z1),
'line2':simplejson.dumps(line_z2)
},
context_instance=RequestContext(request))
else:
form = UploadFileForm()
return render_to_response('flot.html',
{'form': form,
'error': err
},
context_instance=RequestContext(request))
else:
form = UploadFileForm()
return render_to_response('flot.html',
{'form': form},
context_instance=RequestContext(request))