gpt4 book ai didi

python - 如何解决NoReverseMatch, no arguments not found? ( Django )

转载 作者:行者123 更新时间:2023-12-05 06:55:16 27 4
gpt4 key购买 nike

编辑:通过如下修改 view.py,我能够实现所需的行为

urlpatterns = [
path("", views.index, name="index"),
path("wiki/<str:entry>", views.entry, name="entry"),
path("edit/<str:page>", views.edit, name="edit"),
path("new", views.new, name="new"),
path("edit/CSS", views.edit, name="edit")
]

但是我不知道,为什么这行得通!!???注意:它不仅适用于条目“CSS”,还适用于其他条目。

模板edit.html

{% extends "encyclopedia/layout.html" %}

{% block title %}
Edit
{% endblock %}

{% block body %}
<form action="{% url 'edit' %}" method="POST">
{% csrf_token %}
<table border="1">
{{ form_newpage }}
</table>
<button type="submit">Save Page</button>
</form>

{% endblock %}

我是 Django 的新手。我收到以下错误:NoReverseMatch: Reverse for 'edit' with no arguments not found. 1 pattern(s) tried: ['edit/(?P<page>[^/]+)$']

具体说明:我正在构建一个类似维基百科的网站,现在想要允许编辑功能。因此我包括

path("edit/<str:page>", views.edit, name="edit"),

进入我的百科全书应用程序 urls.py 中的 urlpatterns。

对于编辑,views.py 如下所示:

def edit(request, page):
return render(request, "encyclopedia/edit.html", {
"form_newpage": NewPageForm(initial={'content': util.get_entry(page), 'title': page}),
"form_searchwiki": SearchWikiForm()
})

当我现在尝试访问 http://127.0.0.1:8000/edit/CSS 时- CSS 是该百科全书的有效条目 - 我收到 NoReverseMatch 错误。在回溯中,局部变量页面的值是“CSS”,所以我不明白为什么 Django 不将“CSS”识别为 url 中的正确参数。

get_entry 函数:

 def get_entry(title):
"""
Retrieves an encyclopedia entry by its title. If no such
entry exists, the function returns None.
"""
try:
f = default_storage.open(f"entries/{title}.md")
return f.read().decode("utf-8")
except FileNotFoundError:
return None

完整回溯:

Traceback (most recent call last):
File "/home/fed/.local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/home/fed/.local/lib/python3.8/site-packages/django/core/handlers/base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/fed/cs/cs50_web/wiki/encyclopedia/views.py", line 59, in edit
return render(request, "encyclopedia/edit.html", {
File "/home/fed/.local/lib/python3.8/site-packages/django/shortcuts.py", line 19, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "/home/fed/.local/lib/python3.8/site-packages/django/template/loader.py", line 62, in render_to_string
return template.render(context, request)
File "/home/fed/.local/lib/python3.8/site-packages/django/template/backends/django.py", line 61, in render
return self.template.render(context)
File "/home/fed/.local/lib/python3.8/site-packages/django/template/base.py", line 170, in render
return self._render(context)
File "/home/fed/.local/lib/python3.8/site-packages/django/template/base.py", line 162, in _render
return self.nodelist.render(context)
File "/home/fed/.local/lib/python3.8/site-packages/django/template/base.py", line 938, in render
bit = node.render_annotated(context)
File "/home/fed/.local/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
return self.render(context)
File "/home/fed/.local/lib/python3.8/site-packages/django/template/loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "/home/fed/.local/lib/python3.8/site-packages/django/template/base.py", line 162, in _render
return self.nodelist.render(context)
File "/home/fed/.local/lib/python3.8/site-packages/django/template/base.py", line 938, in render
bit = node.render_annotated(context)
File "/home/fed/.local/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
return self.render(context)
File "/home/fed/.local/lib/python3.8/site-packages/django/template/loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "/home/fed/.local/lib/python3.8/site-packages/django/template/base.py", line 938, in render
bit = node.render_annotated(context)
File "/home/fed/.local/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
return self.render(context)
File "/home/fed/.local/lib/python3.8/site-packages/django/template/defaulttags.py", line 446, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "/home/fed/.local/lib/python3.8/site-packages/django/urls/base.py", line 87, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/home/fed/.local/lib/python3.8/site-packages/django/urls/resolvers.py", line 685, in _reverse_with_prefix
raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'edit' with no arguments not found. 1 pattern(s) tried: ['edit/(?P<page>[^/]+)$']
[22/Dec/2020 17:20:16] "GET /edit/CSS HTTP/1.1" 500 153425

我现在尝试的(在 views.py 的编辑函数中)是:

return HttpResponse("Hello, world!")

.. 这没有错误。

我认为,根据我现在阅读的内容,错误必须在 urls.py 中?!错误基本上是说编辑/CSS 不匹配,而应该匹配时..?!

最佳答案

据我了解,您没有在您的上下文中传递页面,但您将其命名为标题,因此页面在您的 url 模板标记中没有值(value)

return render(request, "encyclopedia/edit.html", {
"form_newpage": NewPageForm(initial={'content': util.get_entry(page), 'title': page}),
"form_searchwiki": SearchWikiForm()
})

关于python - 如何解决NoReverseMatch, no arguments not found? ( Django ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65412957/

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