Je veux voir la page http://127.0.0.1:8000/bookmark/.

Mais j'y vais, il y a une erreur dans le site Web.

Le message d'erreur est

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

Et mon code est

[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>

[bookmark/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

[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'), 
                                                            
]

Que devrais-je faire? Aidez-moi, s'il vous plaît...

Et je ne sais pas pourquoi je veux juste aller sur le site de signet, mais le code d'erreur dit que base.html a un problème Je viens de passer sur le lien (http://127.0.0.1:8000/bookmark/ page.) , pas de bouton dans base.html

+)

C'est bookmark/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
   

C'est 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>
0
냠냠냠 5 sept. 2020 à 16:01

1 réponse

Meilleure réponse

Je pense que votre problème est la façon dont vous avez défini votre URL dans votre bookmark_list.html (la ligne <li><a href="{% url 'detail' bookmark.id %}">{{bookmark}}</a></li>). C'est parce que vous avez défini app_name dans vos URL et de cette façon, vous devez changer votre ligne de modèle en quelque chose comme <li><a href="{% url 'bookmark:detail' bookmark.id %}">{{bookmark}}</a></li>.

0
Roham 6 sept. 2020 à 04:20