Django 查看值错误

发布于 2024-11-01 22:58:40 字数 1077 浏览 1 评论 0原文

我因为这个错误而碰壁了。我确信我忽略了一些基本的东西,只是似乎无法弄清楚...

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧人九事 2024-11-08 22:58:40
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) )

你的缩进看起来不对吗?

最初,您使用 request != 'POST' 进入视图,该视图永远不会到达底部的 else 语句,因此您不会得到 HttpResponse。

另一件看起来很奇怪的事情是,即使您修复了缩进,您最初也会显示 RegisterForm,并且在发布请求之后,您将 RegisterForm 中的数据放入 GradeForm,如果这不能验证您是否将 GradeForm 传递给您的模板。这是你的意图吗?

同样在你的 urls.py 中,我会添加 / 到:

(r'^sing/register','register' ),)

像:

(r'^sing/register/','register' ),)

除非你希望它匹配(例如):

www.site.com/sing/registerasdf/

我什至可能建议在末尾使用“/$”,如下所示:

(r'^sing/register/

以防止匹配至(例如):

www.site.com/sing/register/asdf/asdf/asdf/
www.site.com/sing/register/asdf/asdf/
www.site.com/sing/register/asdf/
,'register' ),)

以防止匹配至(例如):

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) )

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:

(r'^sing/register','register' ),)

like:

(r'^sing/register/','register' ),)

unless you want it to match (for example):

www.site.com/sing/registerasdf/

i might even suggest using '/$' at the end like this:

(r'^sing/register/

to prevent matches to (for example):

www.site.com/sing/register/asdf/asdf/asdf/
www.site.com/sing/register/asdf/asdf/
www.site.com/sing/register/asdf/
,'register' ),)

to prevent matches to (for example):

靑春怀旧 2024-11-08 22:58:40

从代码来看,只有当它不是 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?

只是一片海 2024-11-08 22:58:40

我认为它是您的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 shortcut redirect

http://docs.djangoproject.com/en/dev/topics/http/shortcuts/#redirect

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文