簡體   English   中英

Django NoReverseMatch 錯誤 - urls.py 路徑似乎匹配

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

在我作為在線課程項目構建的 Django 應用程序中,我期望一個模板(entry.html)中的鏈接指向 urls.py 中的路徑(“編輯”),其中包含 url 中的變量。 這應該在views.py中啟動一個名為edit的function並渲染模板edit.html。

我收到 NoReverseMatch 錯誤( "Reverse for 'edit' with no arguments not found. 1 pattern(s) tried: ['wiki/(?P<entry>[^/]+)/edit$']" )之后單擊 entry.html 中的鏈接。 如果我在開發服務器中的 entry.html 上查看頁面源,我可以看到 url 與 urls.py 中的匹配,但我仍然收到此錯誤。

在下面的示例中,“maggie”是我試圖傳遞的entryTitle的值。

入口.html:


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

{% block body %}

    {{ entry|safe }}

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

{% endblock %}

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")
]

在 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
        })

有沒有人能夠看到我的代碼導致此錯誤的原因? 我很驚訝,因為在查看頁面源代碼時,似乎{% url 'edit entry=entryTitle %}被正確解釋為wiki/maggie/edit ,它存在於 urls.py 中, maggie<str:entry>我收到這個錯誤。

下面是頁面源截圖: 在此處輸入圖像描述

錯誤信息是我的錯。 我認為一個參數可以填充路徑字符串中的兩個變量,但我猜不是。 你有兩個選擇。

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

這應該就像您的entry方法一樣工作。

  1. 將另一個關鍵字值添加到您的路徑
path("wiki/<str:pathentry>/edit/<str:entry>")
<button>
    <a href="{% url 'edit' pathentry='maggie' entry='maggie'%}">Edit</a>
</button>

我推薦第一個,但你比我更了解你的項目。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM