0

我正试图处理上传Django中的文件,我遇到了一个障碍。我的目标是创建一个表单,允许用户上传文本和文件数据,预览该输入,然后发布它。如何在Django模板中发布隐藏的FileFields?

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

我的问题是我的文件formset无法正常工作。当我刚刚提交数据时,没有预览它,就没有问题。但是,有一次,我开始发回到不同的模板,它停止工作。我知道你希望在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))   

在我的预览模板,我包括隐藏的输入,然后POST方法要么返回到编辑页面或出版。我的photo_formset被包含在一个带有display:none属性的div中,因此它是不可见的。

当我从编辑页面POST时,隐藏文本输入通过,但FILE输入不通过。我能做些什么呢?

感谢

回答

2

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

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

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

检查我的答案在这里:

Resubmitting Image in ImageField after Validation Error in Django

我已保存的文件信息存储在了会议,并在窗体的第3步中拉回来了。

  1. 提交#店面名
  2. 预览
  3. 提交#拉文件名

或者,你可以建立一个AJAX的预览或这样?

+0

是的,我假定表单实例会自动填充文件输入。这很有帮助。那么,如果我想显示图像的预览,我将不得不暂时保存它,对吗? – Ben 2011-03-22 19:49:14

+0

没有办法解决它,它似乎 – Ben 2011-03-22 19:50:23

+0

我认为不 - 先保存并稍后再将其保存,将其存储为大量文本或使用ajax预览。我喜欢ajax方法! – 2011-03-23 01:32:02