我正在开发一个类似维基百科的简单应用程序,使用showmedo中的“在20分钟内创建一个wiki”教程。
该应用程序通过您创建一个页面,例如狗,如果它不存在然后您添加有关狗的所有信息,它显示狗和狗的详细信息。
当我尝试编辑同一页面时。页面未在主页面上更新,或者在我的管理页面上更新。
我认为问题在于我的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/
我的views.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>
我的view.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>
答案 0 :(得分: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 + "/")
这是一个快速丑陋的修复,我建议您查看forms和class based views。
答案 1 :(得分: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()
答案 2 :(得分: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
,try
正在设置 try:
page = Page.objects.get(name=page_name)
except Page.DoesNotExist:
page = Page(name=page_name)
page.content = content
page.save()
。
2)您没有保存在{{1}}区块中。
试试这个:
{{1}}
答案 3 :(得分: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 + "/")