Django 查看值错误
我因为这个错误而碰壁了。我确信我忽略了一些基本的东西,只是似乎无法弄清楚...
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) )
我的 urls.py
urlpatterns = patterns('dashboard.dash.views',
(r'^sing/register','register' ),)
我的 settings.py
TEMPLATE_DIRS = (
"/home/django/testing/sing/grade/templates",)
I'm hitting a wall with this error. I'm sure I'm overlooking something basic, just can't seem to figure it out...
ValueError at /sing/register
The view sing.singer.views.grade didn't return an HttpResponse object.
the view file...
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' ),)
my settings.py
TEMPLATE_DIRS = (
"/home/django/testing/sing/grade/templates",)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的缩进看起来不对吗?
最初,您使用 request != 'POST' 进入视图,该视图永远不会到达底部的 else 语句,因此您不会得到 HttpResponse。
另一件看起来很奇怪的事情是,即使您修复了缩进,您最初也会显示 RegisterForm,并且在发布请求之后,您将 RegisterForm 中的数据放入 GradeForm,如果这不能验证您是否将 GradeForm 传递给您的模板。这是你的意图吗?
同样在你的 urls.py 中,我会添加
/
到:像:
除非你希望它匹配(例如):
我什至可能建议在末尾使用“/$”,如下所示:
以防止匹配至(例如):
your indents look off?
Initially you are entering the view with request != 'POST' which will never reach that else statement at the bottom, so you dont get a HttpResponse.
The other thing that looks strange is even if you fix your indents, you show the RegisterForm initially, and after the post request, you put the data from your RegisterForm into a GradeForm, if that doesn't validate you show pass your GradeForm to your template. Is this what you intended?
also in your urls.py I would add
/
to:like:
unless you want it to match (for example):
i might even suggest using '/$' at the end like this:
to prevent matches to (for example):
从代码来看,只有当它不是 POST 请求时才不会返回 HttpResponse。也许您正在执行 GET 操作?
Judging from the code, the only time it does not return a HttpResponse is when it's not a POST request. Maybe you are doing a GET instead?
我认为它是您的
HttpResonseRedirect
。我不能说我经常使用它(如果有的话)。如果我是你,我会尝试快捷方式redirect
http://docs.djangoproject.com/en/dev/topics/http/shortcuts/#redirect
I think its your
HttpResonseRedirect
. I can't say I've used it that often (if at all). If I were you I would try shortcutredirect
http://docs.djangoproject.com/en/dev/topics/http/shortcuts/#redirect