2

我正在使用来自 showmedo 的教程“在 20 分钟内创建一个 wiki”来开发一个类似于 wikipedia 的简单应用程序。

该应用程序的工作原理是您创建一个页面,例如狗,如果它不存在,那么您添加有关狗的所有信息,它会显示狗和狗的详细信息。在此处输入图像描述

当我尝试编辑同一页面时。该页面没有在主页上更新,也没有在我的管理页面上更新。 在此处输入图像描述

我认为问题出在我的 save_page 函数和 edit_page 函数上。因此,当我创建一个页面时,它可以工作,但是当我尝试检索该页面并保存它时。它不保存。

唯一有效的方法是由管理员编辑页面。

我认为问题出在这里

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

这是网站 http://tafe.pythonanywhere.com/wikicamp/Dogs/

我的观点.py

from wiki.models import Page
from django.shortcuts import render_to_response 
from django.http import HttpResponseRedirect
from django.template import RequestContext
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}, 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.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 + "/")

我的 create.html

 <html>
    <head>
        <title>{{page.name}} - Create </title>
    </head>
    <body>
        <h1>{{page_name}} </h1>
        This page does not exist. <a href="{% url wiki:edit page_name %}">Create?     </a>
 </body>
 </html>

我的edit.html

     <html>
    <head>
        <title>{{page_name - Editing</title>
    </head>
    <body>
        <h1>Editing {{page_name}} </h1>
        <form method = "post" action="{% url wiki:save page_name %}"> {% csrf_token %}
                <textarea name="content" rows="20" cols="60">     {{content}}
      </textarea><br/>      
            <input type="submit" value="Save Page"/>
            </form>
        </body>
      </html>

我的视图.html

<html>
    <head>
    <title>{{page_name}}</title>
        </head>
    <body>
        <h1>{{page_name}} </h1>
        {{content}}
        <hr/>
        <a href="{% url wiki:edit page_name %}">Edit this page ?</a>
    </body>
</html>
4

4 回答 4

1

您没有将页面保存在 save_page 中,只有当它不存在时。尝试类似:

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
         page.save()
     except Page.DoesNotExist:
         page = Page(name=page_name, content=content)
         page.save()
    return HttpResponseRedirect("/wikicamp/" + page_name + "/")

这是一个快速的丑陋修复,我建议查看基于表单类的视图

于 2013-03-04T04:20:56.407 回答
1
 try:
     page = Page.objects.get(pk=page_name)
     page.content = content
 except Page.DoesNotExist:
     page = Page(name=page_name, content=content)
     page.save()

我在这里看到两个问题。首先,您试图通过page_name作为主键来检索页面,当您应该搜索name属性时,其次在您成功获取页面并更新其内容后,您忘记保存它。

由于这是一种常见模式,因此 django 中有一个快捷方式get_or_create,它的工作方式如下:

 page, created = Page.objects.get_or_create(name=page_name)
 if created:
    # new page was created
 else:
    # existing page was retrieved

在您的场景中,您只想在任一场景中获取和更新内容。所以我们不需要使用created变量:

 page, created = Page.objects.get_or_create(name=page_name)
 page.content = content
 page.save()
于 2013-03-04T04:21:09.300 回答
1

我注意到两件事:

 try:
     page = Page.objects.get(pk=page_name)
     page.content = content
 except Page.DoesNotExist:
     page = Page(name=page_name, content=content)
     page.save()

1)在try您正在查询的块中,pk并且在except您正在设置name.

2)您没有保存在try块中。

试试这个:

 try:
     page = Page.objects.get(name=page_name)
 except Page.DoesNotExist:
     page = Page(name=page_name)
 page.content = content
 page.save()
于 2013-03-04T04:23:06.143 回答
1

你忘了放 save()

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
       page.save()
    except Page.DoesNotExist:
       page = Page(name=page_name, content=content)
       page.save()
    return HttpResponseRedirect("/wikicamp/" + page_name + "/")
于 2013-03-04T04:35:28.043 回答