How do I POST hidden FileFields in Django template?
I'm attempting to deal with uploading files in Django and I've hit a snag. My goal is to create a form which allows users to upload text and file data, preview that input, and then publish it.
I put all the logic in a single view with multiple templates associated with the various submit buttons. I didn't want to save all of the instances and then create a custom manager for published posts. However, perhaps I should do that.
My issue is that my file formset isn't working properly. When I just submitted the data, without previewing it, it was fine. But, once, I started POSTing back forth to different templates, it stopped working. I know that you want to HttpResponseRedirect after a POST, but i figured my method was fine in this case. I honestly have no clue.
Here's my view logic
def post(request):
page="post"
#check if user has already posted and if so, direct them to their account post history page
try:
user=User.objects.get(pk=request.user.id)
except User.DoesNotExist:
user=''
if user:
try:
post=user.post_set.all()[0]
return HttpResponseRedirect("/profile/post/")
#Here a user who has not posted is shown a form to do so
except IndexError:
postform=PostForm(prefix="post")
PhotoFormSet=modelformset_factory(Post_Photo,exclude=('post',), extra=4, max_num=4)
photo_formset=PhotoFormSet(queryset=Post_Photo.objects.filter(post__lt=0),prefix="phot开发者_Python百科os") #lt 0 is a hack to return an empty query set, so that other photos aren't returned with the form
#Here an anonymous user sees the form, though they can't successfully submit
else:
postform=PostForm(prefix="post")
PhotoFormSet=modelformset_factory(Post_Photo,exclude=('post',), extra=4, max_num=4)
photo_formset=PhotoFormSet(queryset=Post_Photo.objects.filter(post__lt=0),prefix="photos")
if request.method=='POST' and user and request.POST.get('preview'):
photo_formset=PhotoFormSet(request.POST, request.FILES, prefix="photos")
postform=PostForm(request.POST,prefix="post")
if postform.is_valid() and photo_formset.is_valid():
post=postform.save(commit=False)
photos=photo_formset.save(commit=False)
return render_to_response('website/preview_post.html', {'page':page,'post':post,'photos':photos,'photo_formset':photo_formset}, context_instance=RequestContext(request))
else:
return HttpResponse('error test')
if request.method=='POST' and user and request.POST.get('edit'):
photo_formset=PhotoFormSet(request.POST, request.FILES, prefix="photos")
postform=PostForm(request.POST,prefix="post")
neighborhood=request.POST.get('post-neighborhood','')
if postform.is_valid() and photo_formset.is_valid():
return render_to_response('website/post_upload.html', {'page':page, 'neighborhood':neighborhood,'postform':postform, 'photo_formset':photo_formset}, context_instance=RequestContext(request))
else:
return HttpResponse(postform.errors)
if request.method=='POST' and user and request.POST.get('publish'):
photo_formset=PhotoFormSet(request.POST, request.FILES, prefix="photos")
postform=PostForm(request.POST,prefix="post")
if postform.is_valid() and photo_formset.is_valid():
post=postform.save(commit=False)
post.user=user
post.save()
photos=photo_formset.save(commit=False)
for photo in photos:
photo.post=post
photo.save()
return HttpResponse('/post_upload/?success=1')
else:
return HttpResponse('%s' %postform.errors)
return render_to_response("website/post_upload.html", {'page':page,'postform':postform,'photo_formset':photo_formset}, context_instance=RequestContext(request))
In my preview template, I include hidden inputs that are then POSTed either back to the edit page or published. My photo_formset is contained in a div with display:none property so it's invisible.
When I POST from the edit page, the hidden text inputs go through but the FILE inputs do not. What can I do about this?
Thanks
My guess is that the file inputs are not being passed into step 2, because <input type="file">
elements don't contain data and don't get prepopulated. <input type="file">
are always blank on load.
You can't share file data between browser refreshes, unless you encode the data into a form text element and decode it or store it in the session.
It's a browser security measure.
Check my answer here:
Resubmitting Image in ImageField after Validation Error in Django
I'd store the saved file information in the session and pull it back out in step 3 of your form.
- Submit # store filename
- Preview
- Submit # pull filename
Or, you could set up an ajax preview or such?
精彩评论