这是我得到的错误:视图myapp.views.view_page没有返回HttpResponse对象
有谁能看到我在这里做错了什么?因为我正在返回一个HttpResponseRedirect,所以我似乎无法弄清楚为什么我会得到该异常。
views.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 + "/")
答案 0 :(得分: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
shortcut自动包含RequestContext
。
答案 1 :(得分:1)
你的缩进是完全错误的。 我不知道你是否已经解决了你的问题,但正如我在你提出的一分钟后在第一条评论中说的那样,你应该做到以下几点:
try
条款中返回了一些内容?except
条款中返回了一些内容?如果你密切关注你的代码,你会发现它吗? :)
答案 2 :(得分: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 + "/")