0

我正在使用来自 showmedo 的视频教程创建一个 wiki 应用程序。据我所知,教程很旧,我正在使用 django 1.4.3

当我尝试将页面保存到 wiki 首页时遇到错误。

ValueError at /wikicamp/start/save/

The view wiki.views.save_page didn't return an HttpResponse object.

Request Method:     GET
Request URL:    http://127.0.0.1:8000/wikicamp/start/save/
Django Version:     1.4.3
Exception Type:     ValueError
Exception Value:    

The view wiki.views.save_page didn't return an HttpResponse object.

Exception Location:     C:\Python26\Lib\site-packages\django\core\handlers\base.py in get_response, line 129

Traceback Switch to copy-and-paste view

C:\Python26\Lib\site-packages\django\core\handlers\base.py in get_response

                            raise ValueError("The view %s.%s didn't return an HttpResponse object." % (callback.__module__, view_name))

我认为问题出在函数 save_page 下的views.py中

我的观点是:

 from wiki.models import Page
 from django.shortcuts import render_to_response 
 from django.http import HttpResponseRedirect
 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})
     content = page.content
     return render_to_response("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_to_response("edit.html",{"page_name":page_name, "content":content})
 def save_page(request , page_name):
     content = request.POST.get('content', 'this is the default')
     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("/wikicamp/" + page_name + "/")
4

1 回答 1

0

好吧,是的,如果页面确实存在,您的save_page视图不会返回任何内容。

很可能您只是不小心缩进savereturn低于except

def save_page(request, page_name):
     content = request.POST.get('content', 'this is the default')
     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("/wikicamp/" + page_name + "/")
于 2013-02-23T11:31:59.643 回答