首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >/上的Django NoReverseMatch是否与路径名和/或顺序冲突?

/上的Django NoReverseMatch是否与路径名和/或顺序冲突?
EN

Stack Overflow用户
提问于 2020-09-04 00:39:35
回答 1查看 121关注 0票数 2

你好,我正在尝试添加一个编辑页面到我的django url模式,页面给我一个错误,一旦我点击我的页面上的一个链接,系统崩溃,我从系统中得到一个没有反向匹配的错误。如果我确实将编辑路径与入口路径交换,那么它确实可以工作,但这不是我希望它工作的方式。我希望用户被呈现在第一个入口页面,然后能够点击编辑按钮,以编辑页面。下面是我的观点和一些html页面。

django的新手,并查看了文档,但没有找到任何东西。谢谢你的帮助

下面也是错误:

错误-未找到带参数'('# Django\r\n\r\nDjango是使用Python编写的web框架,它允许设计动态生成HTML的web应用程序。\r\n‘,)’的反向操作。尝试了1个模式:['(?P^/+)$']

urls.py:

代码语言:javascript
复制
from django.urls import path

from . import views

app_name = "enc"
urlpatterns = [
    path("", views.index, name="index"),
    path("new_page", views.new_page,name="check_page"),
    path("",views.get_search,name="search"),
    path("<str:title>",views.entry, name="page"),
    path("<str:title>",views.edit, name ="edit"),
    
]

views.py

代码语言:javascript
复制
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from . import util


entries = {}

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


def entry(request, title):
    if util.get_entry(title):
        return render(request,"encyclopedia/entry.html", {
            "title": util.get_entry(title),
            "name":title
        })
    
    else:
        return render(request, "encyclopedia/error.html")

def get_search(request):
    if request.method == 'GET':
        form = util.search_form(request.GET['q'])
        
        if form.is_valid():
            search  = form.cleaned_data["search"]
            return render(request, "encyclopedia/entry.html",{
                "search":util.get_entry(search)
            })

def new_page (request):
    if request.method == 'POST':
        form = util.new_entry(request.POST)

        if form.is_valid():
            title = form.cleaned_data['title']
            details = form.cleaned_data['details']

            if title in entries:
                return render (request, "encyclopedia/error.html")
            
            else:
                util.save_entry(title, details)
                entries[title] = details
                return HttpResponseRedirect(f'{title}')


    return render(request, "encyclopedia/new_page.html",{
        "form": util.new_entry()
    })



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

    if entry == None:
        return render(request, "encyclopedia/error.html")

    else:

        form = util.edit(initial={'title':title, 'details':entry})


        return render (request, "encyclopedia/edit.html", {
            "form": form,
            'title': title,
            'details':util.get_entry(title),
        } )

用于编辑的html:

代码语言:javascript
复制
{% extends "encyclopedia/layout.html" %}

{% block title %}
    Encyclopedia
{% endblock %}

{% block body %}

    <form action="{% url 'enc:edit' title %}" method="POST">
        {% csrf_token %}
        {{ form.as_p }}
        <input type="submit">
    </form>

{% endblock %}

用于条目的html

代码语言:javascript
复制
{% block title %}
    {{name}}
{% endblock %}

{% block body %}

    <p>{{title}}</p>

    <a href="{% url 'enc:index' %}">Go back to main page</a>
    <a href="{% url 'enc:edit' title %}">Click here to edit the page</a>

{% endblock %}
EN

回答 1

Stack Overflow用户

发布于 2020-09-04 00:51:29

如果两个视图的路径相同,Django会抛出一个错误。试着这样来解决这个问题。

代码语言:javascript
复制
urlpatterns = [
    path("", views.index, name="index"),
    path("new_page/", views.new_page,name="check_page"),
    path("search/",views.get_search,name="search"),
    path("new/<str:title>/",views.entry, name="page"),
    path("edit/<str:title>/",views.edit, name ="edit"),
    
]

注意/前面的斜杠

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63728109

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档