Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango: Uploading photo and word doc on same form not working correctly
    primarykey
    data
    text
    <p>I have an upload form that will allow a user to upload a photo, a word doc, or both. I need help with the logic so that as long they have a photo OR a document selected for uploading, the form is valid and the upload will work. It always works when I have BOTH a photo and doc, but appears to randomly work when it's just a photo or document. Here is my current code:</p> <pre><code>def upload(request): """ Uploads document/photo """ if request.method == 'POST': form1 = DocumentForm(request.POST, request.FILES) form2 = PhotoForm(request.POST, request.FILES) if form1.is_valid() and form2.is_valid() : post1 = Document(user = request.user, document= form1.cleaned_data['document'], title = form1.cleaned_data['title']) post1.save() post2 = Photo(user = request.user, alias = request.user.username, img = form2.cleaned_data['img'], title = "") post2.save() return HttpResponse(template.Template(''' &lt;html&gt;&lt;head&gt;&lt;title&gt;Uploaded&lt;/title&gt;&lt;/head&gt; &lt;body&gt; &lt;h1&gt;Uploaded&lt;/h1&gt; &lt;/body&gt;&lt;/html&gt; ''' ).render( template.Context({})) ) elif form1.is_valid(): post1 = Document(user = request.user, document = form1.cleaned_data['document'], title = form1.cleaned_data['title']) post1.save() return HttpResponse(template.Template(''' &lt;html&gt;&lt;head&gt;&lt;title&gt;Uploaded&lt;/title&gt;&lt;/head&gt; &lt;body&gt; &lt;h1&gt;Uploaded&lt;/h1&gt; &lt;/body&gt;&lt;/html&gt; ''' ).render( template.Context({})) ) else: if form2.is_valid(): post2 = Photo(user = request.user, alias = request.user.username, img = form2.cleaned_data['img'], title = "") post2.save() return HttpResponse(template.Template(''' &lt;html&gt;&lt;head&gt;&lt;title&gt;Uploaded&lt;/title&gt;&lt;/head&gt; &lt;body&gt; &lt;h1&gt;Uploaded&lt;/h1&gt; &lt;/body&gt;&lt;/html&gt; ''' ).render( template.Context({})) ) else: form1 = DocumentForm() form2 = PhotoForm() return render_to_response('upload.html', {'form1': form1, 'form2':form2 }, context_instance=RequestContext(request)) </code></pre> <p>I know there has to be a better way. Any help would be greatly appreciated. Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload