【问题标题】:How do I fix the NoReverseMatch Django error?如何修复 NoReverseMatch Django 错误?
【发布时间】:2020-10-05 13:24:24
【问题描述】:

我正在尝试完成一个 CS50 项目,我想渲染一个 HTML 页面,模板位于相应的 templates/encyclopedia 目录中。

url 127.0.0.1:8000/wiki/HTML 的错误页面在 wiki/HTML 上显示 NoReverseMatch。 未找到“编辑”的反向。 'edit' 不是有效的视图函数或模式名称。

负责的URLS.py"wiki/<str:search>""wiki/random"

很抱歉,我不想在这里发布这么长的代码,但我已经被这个问题困扰了好几个星期了。 试了很多方法不知道这段代码有什么问题。

以下是我的 URLS.py,项目索引 url 包括以下 url:

from django.urls import path
from . import views

urlpatterns = [
    path("", views.index, name="index"),
    path("wiki/newpage", views.newpage, name="newpage"),
    # path("wiki/<str:edit>/edit", views.edit, name="edit"),
    path("wiki/random", views.random, name="random"),
    path("wiki/<str:search>", views.search, name="search"),
    path("wiki/", views.find, name="find")
]

以下是我的VIEWS.py:

from django.shortcuts import render, redirect
from django.contrib import messages
import markdown2 as md
from . import util
from django import forms
import re, random as rand

def index(request):
    return render(request, "encyclopedia/index.html", {
        "entries": util.list_entries()
    })

def search(request, search):
    if search in util.list_entries():
        html = md.markdown(util.get_entry(search))
        return render(request, "encyclopedia/search.html", {
            "search": search, "html": html
        })
    else:
        return render(request, "encyclopedia/error.html")

def random(request):
    randomPage = rand.choice(util.list_entries())
    if randomPage in util.list_entries():
        html = md.markdown(util.get_entry(randomPage))
        return render(request, "encyclopedia/search.html", {
            "random": randomPage, "html": html
        })
    else:
        return render(request, "encyclopedia/error.html")

def find(request):
    if request.method == "GET":
        query = request.GET.get('q')
        for queryMatch in util.list_entries():
            if query.casefold() == queryMatch.casefold():
                html = md.markdown(util.get_entry(query))
                return render(request, "encyclopedia/search.html", {
                    "queryMatch": queryMatch, "html": html
                })
        regex = re.compile(query.casefold())
        matchList = []
        for a in util.list_entries():
            if regex.match(a.casefold()):
                matchList.append(a)
        if not matchList:
            matchList = util.list_entries()
        return render(request, "encyclopedia/list.html", {
            "match": matchList
        })

class NewPageForm(forms.Form):
    title = forms.CharField(label="Title")
    content = forms.CharField(label="Markdown Content", widget=forms.Textarea(attrs={'cols': '100'}))

def newpage(request):
    if request.method == "POST":
        form = NewPageForm(request.POST)
        if form.is_valid():
            title = form.cleaned_data["title"]
            content = form.cleaned_data["content"]
            if title in util.list_entries():
                messages.error(request, f"'{title}' page title already exists!!\nPlease type another title.")
                return render(request, "encyclopedia/newpage.html", {
                    "form": form
                })
            else:
                util.save_entry(title, content)
                return redirect(f"/wiki/{title}")
        else:
            return render(request, "encyclopedia/newpage.html", {
                "form": form
            })
    return render(request, "encyclopedia/newpage.html", {
        "form": NewPageForm()
    })

# def edit(request, edit):
#     entry = util.get_entry(edit)

【问题讨论】:

  • 哪个 Django 版本?名称为“find”的 URL 是否有效?
  • @NKSM 不,它不起作用并显示相同的错误。
  • 你的项目目录结构是什么?您是否将 app.urls 包含在 project.urls 中?

标签: django url view cs50


【解决方案1】:

您是否将您的app.urls 包含在project.urls 中?

Including other URLconfs

Django 教程中的一些示例:

polls/urls.py

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

mysite/urls.py

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
]

【讨论】:

  • 这是一个反问句吗? :)
  • @Scratte,时间会证明一切的:)
  • @NKSM 是的,我已将我的应用 URLs.py 包含在我的项目中。
猜你喜欢
  • 2018-08-12
  • 2012-10-01
  • 2017-12-12
  • 2020-12-05
  • 2018-04-21
  • 2017-11-26
  • 2015-10-24
  • 2017-08-05
相关资源
最近更新 更多