英文:
Why does Django raise a NoReverseMatch error when using the 'entry' iterator in a For loop and url method?
问题
以下是您提供的代码部分的中文翻译:
{% extends "encyclopedia/layout.html" %}
{% block title %}
百科全书
{% endblock %}
{% block body %}
<h1>所有页面</h1>
<ul>
{% for entry in entries %}
<li><a href="{% url 'entry' title=entry %}">{{ entry }}</a></li>
{% comment %} <p> {{ entry }} </p> {% endcomment %}
{% endfor %}
</ul>
{% comment %} <a href="/wiki/{{ entry }}"> <li>{{ entry }}</li> </a> {% endcomment %}
{% endblock %}
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("wiki/<str:title>", views.entry, name="entry"),
path("search/", views.search, name="search"),
path("new/", views.new_page, name="new_page"),
path("edit/", views.edit, name="edit"),
path("save_edit/", views.save_edit, name="save_edit"),
path("rand/", views.rand, name="rand")
]
from django.shortcuts import render
import markdown
from . import util
import random
def convert_md_to_html(title):
content = util.get_entry(title)
markdowner = markdown.Markdown()
if content == None:
return None
else:
return markdowner.convert(content)
def index(request):
return render(request, "encyclopedia/index.html", {
"entries": util.list_entries()
})
def entry(request, title):
html_content = convert_md_to_html(title)
if html_content == None:
return render(request, "encyclopedia/error.html",{
"message": "该条目不存在"
})
else:
return render(request, "encyclopedia/entry.html",{
"title": title,
"content": html_content
})
def search(request):
if request.method == "POST":
entry_search = request.POST['q']
html_content = convert_md_to_html(entry_search)
if html_content is not None:
return render(request, "encyclopedia/entry.html",{
"title": entry_search,
"content": html_content
})
else:
allEntries = util.list_entries()
recommendation = []
for entry in allEntries:
if entry_search.lower() in entry.lower():
recommendation.append(entry)
return render(request, "encyclopedia/search.html", {
"recommendation": recommendation
})
def new_page(request):
if request.method == "GET":
return render(request, "encyclopedia/new.html")
else:
title = request.POST['title']
content = request.POST['content']
titleExist = util.get_entry(title)
if titleExist is not None:
return render(request, "encyclopedia/error.html", {
"message": "该条目已存在"
})
else:
util.save_entry(title, content)
html_content = convert_md_to_html(title)
return render(request, "encyclopedia/entry.html", {
"title": title,
"content": html_content
})
def edit(request):
if request.method == 'POST':
title = request.POST['entry_title']
content = util.get_entry(title)
return render(request, "encyclopedia/edit.html",{
"title": title,
"content": content
})
def save_edit(request):
if request.method == "POST":
title = request.POST['title']
content = request.POST['content']
util.save_entry(title, content)
html_content = convert_md_to_html(title)
return render(request, "encyclopedia/entry.html",{
"title": title,
"content": html_content
})
def rand(request):
allEntries = util.list_entries()
rand_entry = random.choice(allEntries)
html_content = convert_md_to_html(rand_entry)
return render(request, "encyclopedia/entry.html",{
"title" : rand_entry,
"content" : html_content
})
请注意,代码中的中文翻译只是将英文内容翻译成中文,但并没有更改代码逻辑。如果您需要进一步的帮助或解释,请随时提出。
英文:
NoReverseMatch at /
Reverse for 'entry' with keyword arguments '{'title': ''}' not found. 1 pattern(s) tried:['wiki/(?P<title>[^/]+)\Z'] Line 12.
{% extends "encyclopedia/layout.html" %}
{% block title %}
Encyclopedia
{% endblock %}
{% block body %}
<h1>All Pages</h1>
<ul>
{% for entry in entries %}
<li><a href="{% url 'entry' title=entry %}">{{ entry }}</a></li>
{% comment %} <p> {{ entry }} </p> {% endcomment %}
{% endfor %}
</ul>
{% comment %} <a href "/wiki/{{ entry }}"> <li>{{ entry }}</li> </a> {% endcomment %}
{% endblock %}
I am trying to print a list of entries with their links, but Django complains about the iterator 'entry' in the For cycle, it can actually print the elements of the list, but when using the url django method, it won't render the page 'cause that error.
urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("wiki/<str:title>", views.entry, name="entry"),
path("search/", views.search, name="search"),
path("new/", views.new_page, name="new_page"),
path( "edit/", views.edit, name="edit"),
path( "save_edit/", views.save_edit, name="save_edit"),
path( "rand/", views.rand, name= "rand")
]
views.py
from django.shortcuts import render
import markdown
from . import util
import random
def convert_md_to_html(title):
content = util.get_entry(title)
markdowner = markdown.Markdown()
if content == None:
return None
else:
return markdowner.convert(content)
def index(request):
return render(request, "encyclopedia/index.html", {
"entries": util.list_entries()
})
def entry(request, title):
html_content = convert_md_to_html(title)
if html_content == None:
return render(request, "encyclopedia/error.html",{
"message": "This entry does not exist"
})
else:
return render(request, "encyclopedia/entry.html",{
"title": title,
"content": html_content
})
def search(request):
if request.method == "POST":
entry_search = request.POST['q']
html_content = convert_md_to_html(entry_search)
if html_content is not None:
return render(request, "encyclopedia/entry.html",{
"title": entry_search,
"content":html_content
})
else:
allEntries = util.list_entries()
recommendation = []
for entry in allEntries:
if entry_search.lower() in entry.lower():
recommendation.append(entry)
return render(request, "encyclopedia/search.html", {
"recommendation": recommendation
})
def new_page(request):
if request.method == "GET":
return render(request, "encyclopedia/new.html")
else:
title = request.POST['title']
content = request.POST['content']
titleExist = util.get_entry(title)
if titleExist is not None:
return render(request, "encyclopedia/error.html", {
"message": "Entry page already exist"
})
else:
util.save_entry(title, content)
html_content = convert_md_to_html(title)
return render(request, "encyclopedia/entry.html", {
"title": title,
"content": html_content
})
def edit(request):
if request.method == 'POST':
title = request.POST['entry_title']
content = util.get_entry(title)
return render(request, "encyclopedia/edit.html",{
"title": title,
"content": content
})
def save_edit(request):
if request.method == "POST":
title = request.POST['title']
content = request.POST['content']
util.save_entry(title, content)
html_content = convert_md_to_html(title)
return render(request, "encyclopedia/entry.html",{
"title": title,
"content": html_content
})
def rand(request):
allEntries = util.list_entries()
rand_entry = random.choice(allEntries)
html_content = convert_md_to_html(rand_entry)
return render(request, "encyclopedia/entry.html",{
"title" : rand_entry,
"content" : html_content
})
答案1
得分: 1
根据错误消息所说,其中一个条目是空字符串''
。空字符串与任何URL参数都不匹配。
查看您的视图函数,似乎故意允许空字符串。如果是这样,您可以尝试使用re_path
:
from django.urls import re_path
urlpatterns = [
# regular paths
re_path(r'^entry/(?P<title>[\w]*)$', views.entry, name='entry')
]
请注意,\w
将匹配字母数字字符以及下划线。如果您的条目包含其他字符,例如破折号-
,则您需要更改这个正则表达式。
英文:
As the error message says, one of the entries is an empty string ''
. An empty string does not match with any url parameter.
Looking into your view function, en empty string seems to be intentionally allowed. If so, you can try re_path
from django.urls import re_path
urlpatterns = [
# regular paths
re_path(r'^entry/(?P<title>[\w]*)$', views.entry, name='entry')
]
Please note \w
will match alphanumeric characters as well as underscore. If your entry contains other characters, e.g. dash -
, you will have to change this re expression.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论