我是django的新手,我有上传文件的问题请帮帮我! :X
这是我的view.py
def uploadimg(请求):
尝试:
user = request.session['user'] if request.method == 'POST': form = User_image_form(request.POST, request.FILES) #if form.is_valid(): usr_img = User_image(imgfile = request.FILES['imgfile']) return HttpResponse("yes") #usr_img.user = user usr_img.save() return HttpResponse("yees the first upload is right !! :X") else: return HttpResponse("Noooooo!!!")
除了:
pass
这是我的form.py
class User_image_form(forms.Form):
imgfile = forms.FileField()
这是我的models.py
class User_image(models.Model):
imgfile = models.ImageField(upload_to = 'User-Image') #user = models.ForeignKey(User_account)
我在view.py的问题
usr_img = User_image(imgfile = request.FILES ['imgfile'])
并且它永远不会到达
返回HttpResponse(“是”)
错误:
异常值:视图User.views.uploadimg未返回HttpResponse对象。
Plz帮助
答案 0 :(得分:1)
如果存在异常,则表示您未返回HttpResponse
个对象。因此错误。
使用form.is_valid()
查看表单是否有效。
像这样:
if request.method == 'POST':
form = User_image_form(request.POST, request.FILES)
if form.is_valid():
usr_img = User_image(imgfile = form.cleaned_data['imgfile'])
usr_img.user = user
usr_img.save()
return HttpResponse("yees the first upload is right !! :X")
else:
print form.errors #for debugging purposes only.
return HttpResponse("Noooooo!!!")