我正在尝试处理在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 回答
我的猜测是文件输入没有传递到第2步,因为
<input type="file">
元素没有预先填充 .<input type="file">
在加载时始终为空白 .除非将数据编码为表单文本元素并对其进行解码或将其存储在会话中,否则无法在浏览器刷新之间共享文件数据 .
这是一种浏览器安全措施 .
在这里查看我的答案:
Resubmitting Image in ImageField after Validation Error in Django
我将保存的文件信息存储在会话中,然后在表单的第3步中将其拉回 .
提交#store filename
预览
提交#draw文件名
或者,您可以设置ajax预览等?