简体   繁体   English

Django NoReverseMatch 错误 - urls.py 路径似乎匹配

[英]Django NoReverseMatch error - urls.py path appears to match

In a Django application I am building as a project for an online course, I am expecting a link in one template (entry.html) to direct to a path ("edit") in urls.py with a variable in the url.在我作为在线课程项目构建的 Django 应用程序中,我期望一个模板(entry.html)中的链接指向 urls.py 中的路径(“编辑”),其中包含 url 中的变量。 This should initiate a function called edit in views.py and render the template edit.html.这应该在views.py中启动一个名为edit的function并渲染模板edit.html。

I am getting a NoReverseMatch error ( "Reverse for 'edit' with no arguments not found. 1 pattern(s) tried: ['wiki/(?P<entry>[^/]+)/edit$']" ) after clicking the link in entry.html.我收到 NoReverseMatch 错误( "Reverse for 'edit' with no arguments not found. 1 pattern(s) tried: ['wiki/(?P<entry>[^/]+)/edit$']" )之后单击 entry.html 中的链接。 If I view page source while on entry.html in the development server, I can see the url matches that in urls.py but I still get this error.如果我在开发服务器中的 entry.html 上查看页面源,我可以看到 url 与 urls.py 中的匹配,但我仍然收到此错误。

In the example below, "maggie" is the value for entryTitle I'm trying to pass.在下面的示例中,“maggie”是我试图传递的entryTitle的值。

entry.html:入口.html:


{% block title %}
    {{ entryTitle }}
{% endblock %}

{% block body %}

    {{ entry|safe }}

    <button>
        <a href="{% url 'edit' entry=entryTitle %}">Edit Entry</a>
    </button>

{% endblock %}

urls.py (last path listed is edit ) urls.py (列出的最后一个路径是edit

from django.urls import path

from . import views

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

edit function in views.py also displaying my entry function在 views.py 中编辑 function也显示我的entry function

from django.http import HttpResponseRedirect
from django.urls import reverse

class EditPageForm(forms.Form):
    content = forms.CharField(
        widget=forms.Textarea(),
        label="Edit Content:")

def edit(request, entry):
    if request.method == "POST":
        #Edit file and redirect
        form = EditPageForm(request.POST)
        if form.is_valid():
            content = form.cleaned_data["content"]
            util.save_entry(entry, content)
            return HttpResponseRedirect(reverse('entry', kwargs={'entry': entry}))
    else:
        #Load form with initial values filled
        content = util.get_entry(entry)
        form = EditPageForm(initial={"content": content})
        return render(request, "encyclopedia/edit.html", {
            "editform": form,
            "entryTitle": entry
        })

def entry(request, entry):
    markdowner = Markdown()
    entryPage = util.get_entry(entry)
    if entryPage is None:
        return render(request, "encyclopedia/notfound.html", {
            "entryTitle": entry
                      })
    else:
        return render(request, "encyclopedia/entry.html", {
            "entry": markdowner.convert(entryPage),
            "entryTitle": entry
        })

Is anyone able to see what is causing this error with my code?有没有人能够看到我的代码导致此错误的原因? I am surprised because when viewing page source, it seems that {% url 'edit entry=entryTitle %} is being correctly interpreted as wiki/maggie/edit , which is present in urls.py with maggie as <str:entry> and yet I am getting this error.我很惊讶,因为在查看页面源代码时,似乎{% url 'edit entry=entryTitle %}被正确解释为wiki/maggie/edit ,它存在于 urls.py 中, maggie<str:entry>我收到这个错误。

Here is a screenshot of page source:下面是页面源截图: 在此处输入图像描述

The error message is my fault.错误信息是我的错。 I thought a single parameter could fill two variables in the path string but I guess not.我认为一个参数可以填充路径字符串中的两个变量,但我猜不是。 You have two options.你有两个选择。

  1. Change your url structure:更改您的 url 结构:
path("wiki/edit/<str:entry>", views.edit, name="edit")
<button>
    <a href="{% url 'edit' entry='maggie'%}">Edit</a>
</button>

This should work just like your entry method.这应该就像您的entry方法一样工作。

  1. Add another keyword value to your path将另一个关键字值添加到您的路径
path("wiki/<str:pathentry>/edit/<str:entry>")
<button>
    <a href="{% url 'edit' pathentry='maggie' entry='maggie'%}">Edit</a>
</button>

I recommend the first but you know more about your project than I do.我推荐第一个,但你比我更了解你的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM