如何在Django模板中发布隐藏的FileField?

时间:2011-03-22 19:20:57

标签: django django-models django-templates django-forms django-views

我正在尝试处理在Django中上传文件,我遇到了麻烦。我的目标是创建一个表单,允许用户上传文本和文件数据,预览输入,然后发布它。

我将所有逻辑放在一个视图中,其中多个模板与各种提交按钮相关联。我不想保存所有实例,然后为已发布的帖子创建自定义管理器。但是,也许我应该这样做。

我的问题是我的文件formset工作不正常。当我刚刚提交数据而没有预览时,它很好。但是,有一次,我开始POST回到不同的模板,它停止工作。我知道你想在POST后想要HttpResponseRedirect,但我认为我的方法在这种情况下很好。老实说,我没有任何线索。

这是我的观点逻辑

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="photos")  #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))        

在我的预览模板中,我包含隐藏的输入,然后将其发布回编辑页面或发布。我的photo_formset包含在一个带有display:none属性的div中,因此它是不可见的。

当我从编辑页面发布POST时,隐藏的文本输入会通过,但FILE输入不会。我该怎么办?

由于

1 个答案:

答案 0 :(得分:2)

我的猜测是文件输入没有传递到第2步,因为<input type="file">元素不包含数据而且没有预先填充。 <{1}}在加载时始终为空白。

除非您将数据编码为表单文本元素并对其进行解码或将其存储在会话中,否则无法在浏览器刷新之间共享文件数据。

这是一种浏览器安全措施。

在这里查看我的答案:

Resubmitting Image in ImageField after Validation Error in Django

我会将保存的文件信息存储在会话中,并在表单的第3步中将其拉回。

  1. 提交#store filename
  2. 预览
  3. 提交#draw文件名
  4. 或者,您可以设置ajax预览等吗?