gpt4 book ai didi

python - Django: View 中的冲突行为

转载 作者:太空宇宙 更新时间:2023-11-03 18:43:33 25 4
gpt4 key购买 nike

我有一个 wiki 应用程序,具有存储、保存、编辑功能。当我创建一个 wiki 页面时,它会被完美存储,并且 view_page View 在从数据库获取数据并将其显示在浏览器中时没有任何问题。但是当我单击编辑按钮并且它进入 edit_page View 时,问题就出现了,该 View 没有获取数据。它获取标题,但不获取内容并引发 Article.DoesNotExist 异常。

这是两个 View :

def view_page(request):
if "title" in request.GET and request.GET["title"]:
article_name = request.GET["title"]
try:
article = Article.objects.get(title=article_name)
except Article.DoesNotExist:
return render_to_response("search_page.html", {"error": "page doesn't exist"})
return render_to_response("view_page.html", {"title": article.title, "content": article.content})
else:
return render_to_response("search_page.html", {"error": "page doesn't exist."})


def edit_page(request):
c = {}
c.update(csrf(request))
if "title" in request.GET:
article_name = request.GET["title"]
try:
article = Article.objects.get(title=article_name)
content = article.content
except Article.DoesNotExist:
content = ""

c["title"] = article_name
c["content"] = content
return render_to_response("edit_page.html", c)

def save_page(request):
if request.method == 'POST':

if "title" in request.POST and "content" in request.POST and request.POST["title"]:

c = {}
c.update(csrf(request))
try:
article = Article.objects.get(title=request.POST["title"])
article.content = request.POST["content"]
except Article.DoesNotExist:
article = Article(request.POST["title"], request.POST["content"])
article.save()
return HttpResponseRedirect("/view_page/?title="+request.POST["title"])
return render_to_response("create_page.html", c)

这是我的模型:

class Article(models.Model):
title = models.CharField(primary_key=True, unique=True, max_length=20)
content = models.TextField(blank=True)

最佳答案

看起来问题可能出在 save_page() View 的 try-catch 部分的 try block 中,请尝试以下操作:

def save_page(request):
if request.method == 'POST':

if "title" in request.POST and "content" in request.POST and request.POST["title"]:

c = {}
c.update(csrf(request))
try:
article = Article.objects.get(title=request.POST["title"])
article.content = request.POST["content"]
article.save()
except Article.DoesNotExist:
article = Article(request.POST["title"], request.POST["content"])
article.save()
return HttpResponseRedirect("/view_page/?title="+request.POST["title"])
return render_to_response("create_page.html", c)

以前,如果文章以前存在,则在内容更新后不会保存该文章。

编辑:您可以尝试使用以下内容调试 edit_page() View :

def edit_page(request):
c = {}
c.update(csrf(request))
if "title" in request.GET:
article_name = request.GET["title"]
try:
article = Article.objects.get(title=article_name)
content = article.content
print "article: %s retrieved from database" % article_name
except Article.DoesNotExist:
print "article: %s not found in database" % article_name
content = ""

c["title"] = article_name
c["content"] = content
return render_to_response("edit_page.html", c)

如果您使用./manage.py runserver,调试消息应该与http请求混合出现,如果数据库正确检索文章,则模板一定有问题。

关于python - Django: View 中的冲突行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20022095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com