我正在开展一个项目,目前我的任务是允许用户上传新图片。我编辑了我在这里找到的教程中的代码:Need a minimal Django file upload example。我最初遵循教程,只是将“文件”更改为“图像”,因为我只允许图像上传,并且工作正常。然后,我试图将它折叠成现有的Django项目,我遇到了我的问题:虽然我已经包含了所有的错误块,但是当我尝试上传不是图像的东西时,我没有收到任何错误消息。我已经包含了将图像标记为“公共”的功能,但即使我将其取出,也不会显示错误消息。为了使这项工作与项目的其余部分一起工作,我不得不将这些内容分开,而不是在教程中,我想知道这是否是问题。
以下项目的相关代码。
模板
<form action="{% url "upload_image" %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<p>{{ form.non_field_errors }}</p>
<p>{{ form.imagefile.label_tag }} {{ form.imagefile.help_text }}</p>
<p>
{{ form.imagefile.errors }}
{{ form.imagefile }}
</p>
<p> {{ form.errors }}</p>
<p>{{ form.public }} {{ form.public.label }}</p>
<p><input type="submit" value="Upload" /></p>
</form>
表单
(我这里有两个表格,因为我无法弄清楚如何检查复选框的值。)
class ImageForm(Form):
imagefile=ImageField(label="Select image to upload.")
public=BooleanField(label="Public image", required=False)
class PublicImageForm(ImageForm):
public=BooleanField(label="Public image", required=True)
查看以呈现包含表单的页面
def upload_image_view(request):
if request.method=='GET':
form=ImageForm()
# TODO: Limits to images uploaded by current user
my_images = UploadedImage.objects.filter(owner=User.objects.get(username=request.user))
return render_to_response(
'uploader.html',
{'my_images': my_images, 'form': form,},
context_instance=RequestContext(request))
else:
return HttpResponseNotAllowed(['GET'])
查看哪种表单
def upload_new_image(request):
if request.method=='POST':
form = ImageForm(request.POST, request.FILES)
if form.is_valid():
u= User.objects.get(username=request.user)
form = PublicImageForm(request.POST, request.FILES)
if form.is_valid():
newdoc = UploadedImage(imagefile = request.FILES['imagefile'], owner=u, isPublic=True)
else:
newdoc = UploadedImage(imagefile = request.FILES['imagefile'], owner=u, )
newdoc.save()
# else:
# form = ImageForm()
return HttpResponseRedirect('/workspace/upload_image/')
else:
return HttpResponseNotAllowed(['POST'])
模型
class UploadedImage(models.Model):
imagefile=models.ImageField(upload_to="user_images/uploaded_images")
owner=models.ForeignKey(User)
isPublic=models.BooleanField(default=False)
答案 0 :(得分:1)
您只是在表单无效时重定向。所以任何错误都会丢失,什么都不会发生。我建议你把这两个观点结合起来。 (没有理由将它们分开。)然后,当表单无效时,您可以返回相同的视图。这是我的粗略实现:(未经测试)
def upload_image_view(request):
if request.method=='GET':
form=ImageForm()
# TODO: Limits to images uploaded by current user
my_images = UploadedImage.objects.filter(owner=User.objects.get(username=request.user))
return render_to_response(
'uploader.html',
{'my_images': my_images, 'form': form,},
context_instance=RequestContext(request)
)
elif request.method=='POST':
form = ImageForm(request.POST, request.FILES)
if form.is_valid():
u= User.objects.get(username=request.user)
public_form = PublicImageForm(request.POST, request.FILES)
if public_form.is_valid():
newdoc = UploadedImage(imagefile = request.FILES['imagefile'], owner=u, isPublic=True)
else:
newdoc = UploadedImage(imagefile = request.FILES['imagefile'], owner=u, )
newdoc.save()
return HttpResponseRedirect('/workspace/upload_image/')
else:
my_images = UploadedImage.objects.filter(owner=User.objects.get(username=request.user))
return render_to_response(
'uploader.html',
{'my_images': my_images, 'form': form,},
context_instance=RequestContext(request)
)
else:
return HttpResponseNotAllowed(['GET', 'POST'])