我在/ accounts / upload_save /收到错误,ValueError 视图accounts.views.upload_save未返回HttpResponse对象。它返回了None。 始终无法正常发送图像。我在views.py中写道
def photo(request):
d = {
'photos': Post.objects.all(),
}
return render(request, 'registration/accounts/profile.html', d)
def upload_save(request):
if request.method == "POST":
print(444)
form = UserImageForm(request.POST, request.FILES)
if form.is_valid():
print(5555)
image1 = form.cleaned_data.get('image1')
image2 = form.cleaned_data.get("image2")
user = request.user
ImageAndUser.objects.create(
User=user,
image=image1,
image2=image2,
image3=image3,
)
return redirect('registration/accounts/photo.html')
else:
print(666)
form = UserImageForm(request.POST or None)
return render(request, 'registration/profile.html',{'form':form})
profile.html中的
<main>
<div>
<img class="absolute-fill">
<div class="container" id="photoform">
<form action="/accounts/upload_save/" method="POST" enctype="multipart/form-data" role="form">
{% csrf_token %}
<div class="input-group">
<label>
<input id="image1" type="file" name="image1" accept="image/*" style="display: none">
</label>
<input type="text" class="form-control" readonly="">
</div>
<div class="input-group">
<label>
<input id="image2" type="file" name="image2" accept="image/*" style="display: none">
</label>
<input type="text" class="form-control" readonly="">
</div>
<div class="input-group">
<label>
<input id="image3" type="file" name="image3" accept="image/*" style="display: none">
</label>
<input type="text" class="form-control" readonly="">
</div>
<div class="form-group">
<input type="hidden" value="{{ p_id }}" name="p_id" class="form-control">
</div>
<input id="send" type="submit" value="SEND" class="form-control">
</form>
</div>
</div>
</div>
</main>
在forms.py
中class UserImageForm(forms.ModelForm):
image = forms.ImageField()
class Meta:
model = ImageAndUser
fields = ('image1','image2','image3')
在urls.py中
urlpatterns = [
url(r'^profile/$', views.profile, name='profile'),
url(r'^photo/$', views.photo, name='photo'),
url(r'^upload_save/$', views.upload_save, name='upload_save'),
]
我真的不明白为什么会发生这种错误。我肯定会发送图片,所以我认为类型不是None。我的代码出了什么问题?我应该如何解决这个问题?我对我的views.py代码进行了调试,所以444显示在print(444).Traceback是 回溯:
File "/Users/xxx/anaconda/envs/py35/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)
File "/Users/xxx/anaconda/envs/py35/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
198. "returned None instead." % (callback.__module__, view_name)
Exception Type: ValueError at /accounts/upload_save/
Exception Value: The view accounts.views.upload_save didn't return an HttpResponse object. It returned None instead.
答案 0 :(得分:1)
首先你的模型看起来像这样: 模型:ImageAndUser包含四个fied:user,image,image2,image3
正如您在对象创建中提到的那样。 ImageAndUser.objects.create(用户=用户,图像= image1的,图像2 =图像2,图像3 =的Image3 )
你提到了表格中的字段:
class UserImageForm(forms.ModelForm):
image = forms.ImageField()
class Meta:
model = ImageAndUser
fields = ('image1','image2','image3')
image1来自哪里。它的图像属性。
这样做。
class UserImageForm(forms.ModelForm):
class Meta:
model = ImageAndUser
fields = ('image','image2','image3')
在模板一侧进行编辑。
<input id="image" type="file" name="image" accept="image/*" style="display: none">
</label>
希望这有效。
答案 1 :(得分:0)
首先,image3
未设置。尝试添加此行
image3 = form.cleaned_data.get("image3")
在对image2
执行相同操作的那个之后。
此外,如果表单无效,则视图将返回None
,即如果form.is_valid()
返回False
。