我遇到了这个错误。我确定我忽略了一些基本的东西,似乎无法弄明白......
ValueError at /sing/register
The view sing.singer.views.grade didn't return an HttpResponse object.
视图文件......
from django.shortcuts import render_to_response
from django import forms
from django.http import HttpResponseRedirect
from django.template import Template, RequestContext
from dash.forms import GradeForm
def register(request):
if request.method == 'POST':
form = GradeForm(data=request.POST)
if form.is_valid():
new_dash_profile = form.save()
new_user = form.save()
return HttpResponseRedirect("/success/")
else:
form = RegisterForm()
return render_to_response('grade.html',{'form':form},context_instance=RequestContext(request) )
my urls.py
urlpatterns = patterns('dashboard.dash.views',
(r'^sing/register','register' ),)
我的settings.py
TEMPLATE_DIRS = (
"/home/django/testing/sing/grade/templates",)
答案 0 :(得分:4)
def register(request):
if request.method == 'POST':
form = GradeForm(data=request.POST)
if form.is_valid():
new_dash_profile = form.save()
new_user = form.save()
return HttpResponseRedirect("/success/")
else:
form = RegisterForm()
return render_to_response('grade.html',{'form':form},context_instance=RequestContext(request) )
你的缩进看了吗?
最初你是用请求进入视图!='POST',它永远不会到达底部的else语句,所以你没有得到HttpResponse。
另一件看起来很奇怪的事情就是即使你修复你的缩进,你最初显示RegisterForm,并在post请求之后,你将RegisterForm中的数据放入GradeForm,如果这不验证你显示通过你的GradeForm到您的模板。这是你的意图吗?
也在您的urls.py中我会将/
添加到:
(r'^sing/register','register' ),)
像:
(r'^sing/register/','register' ),)
除非你想让它匹配(例如):
www.site.com/sing/registerasdf/
我甚至建议在最后使用'/ $',如下所示:
(r'^sing/register/$','register' ),)
阻止匹配(例如):
www.site.com/sing/register/asdf/asdf/asdf/
www.site.com/sing/register/asdf/asdf/
www.site.com/sing/register/asdf/
答案 1 :(得分:1)
从代码判断,它不返回HttpResponse的唯一时间是它不是POST请求。也许你正在做GET呢?
编辑:至于投票:是的,我的答案不是很全面(没有指出错位的缩进),但它指出了实施逻辑中的谬误。
答案 2 :(得分:0)
我认为是你的HttpResonseRedirect
。我不能说我经常使用它(如果有的话)。如果我是你,我会尝试捷径redirect
http://docs.djangoproject.com/en/dev/topics/http/shortcuts/#redirect