gpt4 book ai didi

python - "The view didn' t 返回一个 HttpResponse 对象”

转载 作者:太空宇宙 更新时间:2023-11-04 01:32:53 26 4
gpt4 key购买 nike

这是我遇到的错误: View extraio.file_uploader.views.Upload_File 没有返回 HttpResponse 对象。

谁能看出我在这里做错了什么?我似乎无法弄清楚为什么我会收到该异常,因为我正在返回 HttpResponseRedirect。

from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from models import Files
from forms import Upload_File_Form

def Upload_File(request):
if request.method == 'POST':
form = Upload_File_Form(request.POST, request.FILES)
if form.is_valid():
for f in request.FILES.get_list('file'):
f.save()
orgfilename = Files(orgname=f.name)
orgfilename.save()
return HttpResponseRedirect('success.html')
else:
form = Upload_File_Form()
return render_to_response('upload.html', {'form': form})

最佳答案

您仅在 POST 请求时返回您的 HttpResponse 对象。像这样重写你的 View

def Upload_File(request):
form = Upload_File_Form(request.POST or None,
request.FILES or None)
if request.method == 'POST':
if form.is_valid():
for f in request.FILES.getlist('file'):
f.save()
orgfilename = Files(orgname=f.name)
orgfilename.save()
return HttpResponseRedirect('success.html')
return render_to_response('upload.html', {'form': form},
context_instance=RequestContext(request))

编辑:顺便说一句,您忘记在render_to_response 中设置context_instance您也可以使用 render 代替

render(request, 'upload.html', {'form': form})

request.FILES.get_list('file') 应该是 request.FILES.getlist('file')

关于python - "The view didn' t 返回一个 HttpResponse 对象”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12474344/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com