【问题标题】:NoReverseMatch - Reverse for 'detail' not found. 'detail' is not a valid view function or pattern nameNoReverseMatch - 未找到“详细信息”的反向。 'detail' 不是有效的视图函数或模式名称
【发布时间】:2020-12-24 11:53:48
【问题描述】:

我想查看http://127.0.0.1:8000/bookmark/ 页面。

但是我去那里,网站有错误。

错误信息是

NoReverseMatch at /bookmark/
Reverse for 'detail' not found. 'detail' is not a valid view function or pattern name.
Request Method: GET
Request URL:    http://127.0.0.1:8000/bookmark/
Django Version: 3.1
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'detail' not found. 'detail' is not a valid view function or pattern name.3

我的代码是

[base.html]

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %}django web programming {% endblock %}</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    
    <!-- 하위 html 파일에서 이 부분에 style 태그를 추가할 가능성이 있으므로 block 태그를 기입. 블록태그이름은 extra-style로 지정함 변경가능 -->
    {% block extra-style %}{% endblock %}
    <style>
    .nav-link{
          font-size: 18px;
          font-weight: 500;
        }
      
    </style>
    
</head>
<body style = "padding-top:90px;">

 <!-- home.html (4) 참고  -->
<nav class="navbar navbar-expand-lg navbar-light bg-light fixed-top">
    <span class="navbar-brand mx-5 mb-0 font-weight-bold font-italic"> Motdongsan</span>
    <!-- <a class="navbar-brand" href="#">Navbar</a> -->
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
  
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav mr-auto">
        
        <li class="nav-item mx-3 btn btn-light">
          <a class="nav-link" href="{% url 'home'%}">Home <span class="sr-only">(current)</span></a>
        </li>
        
        <li class="nav-item mx-3 btn btn-light">
          <a class="nav-link" href="{% url 'bookmark:index'%}">Bookmark</a>
        </li>

        <li class="nav-item mx-3 btn btn-light">
            <a class="nav-link" href="{% url 'blog:index'%}">blog</a>
        </li>
  
        <li class="nav-item mx-3 btn btn-light">
            <a class="nav-link" href="{% url 'bookmark:index'%}">photo</a>
        </li>
  
        <li class="nav-item mx-3 btn btn-light">
          <a class="nav-link" href="">Photo</a>
        </li>


        <li class="nav-item dropdown mx-3 btn btn-light">
          <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            Dropdown
          </a>
          <div class="dropdown-menu" aria-labelledby="navbarDropdown">
            <a class="dropdown-item" href="#">Admin</a>
            <a class="dropdown-item" href="#">Archive</a>
            <div class="dropdown-divider"></div>
            <a class="dropdown-item" href="#">Search</a>
          </div>
        </li>
        <li class="nav-item mx-3 btn  ">
          <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
        </li>
      </ul>

      <form class ="form-inline my-2" action="" method="post"> {% csrf_token %}
        <input class="form-control mr-sm-2" type="search" placeholder="global search" name="search_word">
      </form>



      <form class="form-inline my-2 my-lg-0">
        <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
        <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
      </form>
    </div>
</nav>   
    <!-- 본문 내용은 각 페이지마다 달라질수 있으므로 block 태그를 사용. 블록태그이름은 content  -->
<div class="container bg-warning">
    {% block content %}{% endblock %}
</div>


    {% block footer %}{% endblock %}

    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
    <script src="https://kit.fontawesome.com/c998a172fe.js"></script>
    {% block extra-script %}{% endblock %}

</body>
</html>

[bookmark_detail.html]

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Django Bookmark Detail</title>
</head>
<body>
<div id ="content">
    <h1>{{object.title}}</h1>

    <ul>
        <li>URL: <a href="{{object.url}}">{{object.url}}</a></li>
    </ul>
</div>

</body>
</html>

[书签/views.py]

from django.shortcuts import render

# Create your views here.
from django.views.generic import ListView, DetailView
from bookmark.models import Bookmark

class BookmarkLV(ListView):
    model = Bookmark

class BookmarkDV(DetailView):
    model = Bookmark

[书签/urls.py]

from django.urls import path
from bookmark.views import BookmarkLV, BookmarkDV

app_name= 'bookmark' 

urlpatterns = [ 
    path('', BookmarkLV.as_view(), name='index'), 
    path('<int:pk>/', BookmarkDV.as_view(), name='detail'), 
                                                            
]

我该怎么办?请帮帮我...

我不知道为什么我只想去书签网站,但是错误代码说 base.html 有问题 我只是继续链接(http://127.0.0.1:8000/bookmark/ 页面。),而不是 base.html 中的按钮

+)

这是书签/models.py

from django.db import models
# Create your models here.

class Bookmark(models.Model):
    title = models.CharField('TITLE', max_length =100, blank =True)
    url = models.URLField('URL', unique=True)
                        # URL : admin사이트에서 보일것
    def __str__(self):
          return "%s %s" %(self.title, self.url)
        # return self.title
   

这是bookmark_list.html

 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
{% extends "base.html"%}
    {% block title %}bookmark_list.html{% endblock %}
    {% block content %}
    </head>
    
    <body>
    <div id="content">
        <h1> Bookmark List</h1>
        <ul> 
            {%for bookmark in object_list%}
                <li><a href="{% url 'detail' bookmark.id %}">{{bookmark}}</a></li>
            
            {%endfor%}
        </ul>
    {% endblock %}
    </div>    
    </body>
    </html>

【问题讨论】:

  • 也分享您的模型文件
  • 我没有找到你的书签列表页面
  • 看来你不是唯一一个:stackoverflow.com/questions/62116238/…
  • 是的,我在帖子中添加文件
  • 是的,这是某些人发布的错误.. 但我看到他们的帖子,我无法修复我的错误..

标签: html django web


【解决方案1】:

我认为您的问题是您在bookmark_list.html 中定义网址的方式(&lt;li&gt;&lt;a href="{% url 'detail' bookmark.id %}"&gt;{{bookmark}}&lt;/a&gt;&lt;/li&gt; 行)。这是因为您在网址中定义了app_name,因此您应该将模板行更改为&lt;li&gt;&lt;a href="{% url 'bookmark:detail' bookmark.id %}"&gt;{{bookmark}}&lt;/a&gt;&lt;/li&gt;

【讨论】:

  • 天哪,你真聪明。哇。这个怎么运作? ...... 哦!!!!!!!!!!!
猜你喜欢
  • 2019-01-19
  • 1970-01-01
  • 1970-01-01
  • 2019-08-13
  • 2019-05-19
  • 2019-04-21
  • 2019-06-01
  • 2021-11-15
  • 2021-08-14
相关资源
最近更新 更多