0

这是我得到的错误:视图 myapp.views.view_page 没有返回 HttpResponse 对象

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

视图.py

from myapp.models import Page
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import Template, RequestContext
from django.core.context_processors import csrf

def view_page(request, page_name):
        try:
                    page = Page.objects.get(pk=page_name)
        except Page.DoesNotExist:
                    return render_to_response("create.html", {"page_name" :page_name},context_instance=RequestContext(request))

                    content=page.content
                    return render_to_response("view.html", {"page_name" :page_name , "content" :content},context_instance=RequestContext(request))

def edit_page(request, page_name):
        try:
                    page = Page.objects.get(pk=page_name)
                    content=page.content
        except Page.DoesNotExist:
                    content = ""
                    return render_to_response("edit.html", {"page_name" :page_name, "content" :content},context_instance=RequestContext(request))         

def save_page(request, page_name):
                content = request.POST["content"]
        try:
                    page = Page.objects.get(pk=page_name)
                    page.content=content

        except Page.DoesNotExist:
                    page = Page(name=page_name, content=content)
                    page.save()
                    return HttpResponseRedirect("/myproject/" + page_name + "/")
4

3 回答 3

1

你的缩进是完全错误的。我不知道你是否已经解决了你的问题,但正如我在你问一分钟后的第一条评论中所说的那样,你应该执行以下操作:

  • 评论除一种方法外的所有内容
  • 例如保留方法 view_page
  • 仔细看看试试
  • 考虑以下问题:我是否在所有情况下都返回了一些东西?
    • 我是否在try条款上返回一些东西?
    • 我是否在我的except条款中返回一些东西?
    • 我的缩进可能是错误的?

如果您仔细查看您的代码,您会发现它吗?:)

于 2012-10-15T20:11:11.047 回答
1

你有一个没有返回任何东西的执行路径——当没有引发异常时。您所拥有的缩进将两个 return 语句都放在了 except 块中;实际上从不执行第二次返回:

from django.shortcuts import render

def view_page(request, page_name):
    try:
        page = Page.objects.get(pk=page_name)
    except Page.DoesNotExist:
        return render(request,"create.html",{"page_name" :page_name})

    content=page.content
    return render(request,"view.html",{"page_name":page_name,"content":content})

您还需要对其他方法执行相同的操作:

def edit_page(request, page_name):

    try:
        page = Page.objects.get(pk=page_name)
        content=page.content
    except Page.DoesNotExist:
        content = ""
    return render(request,"edit.html", {"page_name":page_name,"content":content})

def save_page(request, page_name):

    content = request.POST["content"]
    page,_ = Page.objects.get_or_create(pk=page_name)
    page.content = content
    page.save()
    return HttpResponseRedirect("/myproject/" + page_name + "/")

我正在使用render自动包含RequestContext.

于 2012-10-15T08:01:50.507 回答
0

您的 view_page 功能是

def view_page(request, page_name):
    try:
        page = Page.objects.get(pk=page_name)
        content=page.content
    except Page.DoesNotExist:
        content=None
    return render_to_response("view.html",{"page_name": page_name,
                                           "content": content},
                              context_instance=RequestContext(request))

同样的编辑和保存方法是

def edit_page(request, page_name):
    try:
        page = Page.objects.get(pk=page_name)
        content=page.content
    except Page.DoesNotExist:
        content = ""
    return render_to_response("edit.html", {"page_name": page_name,
                                            "content": content},
                              context_instance=RequestContext(request))         

def save_page(request, page_name):
    content = request.POST["content"]
    try:
        page = Page.objects.get(pk=page_name)
        page.content=content
    except Page.DoesNotExist:
        page = Page(name=page_name, content=content)
    page.save()
    return HttpResponseRedirect("/myproject/" + page_name + "/")
于 2012-10-15T17:54:56.213 回答