【问题标题】:How do I POST hidden FileFields in Django template?如何在 Django 模板中发布隐藏的 FileFields?
【发布时间】:2011-03-22 19:20:56
【问题描述】:

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

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

我的问题是我的文件表单集无法正常工作。当我刚刚提交数据时,没有预览它,这很好。但是,有一次,我开始向后发布到不同的模板,它停止工作。我知道您想在 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 输入不会。我该怎么办?

谢谢

【问题讨论】:

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


    【解决方案1】:

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

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

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

    在这里查看我的答案:

    Resubmitting Image in ImageField after Validation Error in Django

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

    1. 提交#存储文件名
    2. 预览
    3. 提交#拉取文件名

    或者,您可以设置一个 ajax 预览等?

    【讨论】:

    • 是的,我曾假设表单实例会自动填充文件输入。这真的很有帮助。那么,如果我想显示图像的预览,我将不得不暂时保存它,对吧?
    • 我认为不会——先保存然后再拉出来,将其存储为一大块文本,或者使用 ajax 预览。我喜欢 ajax 方法!
    猜你喜欢
    • 1970-01-01
    • 2011-03-05
    • 1970-01-01
    • 2016-02-14
    • 2016-01-14
    • 2016-03-16
    • 2016-04-18
    • 2013-03-05
    • 2018-10-28
    相关资源
    最近更新 更多