首页 > 解决方案 > NoReverseMatch at / 在服务器的开始

问题描述

它给了我一个错误:NoReverseMatch at / 以 base.html 文件为目标(如图所示)。但是 base.html 文件对我来说看起来不错。

在此处输入图像描述

Base.html

<!DOCTYPE html>
{% load staticfiles %}
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Blog</title>

    <!-- Bootstrap -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">

    <!-- Medium Style editor -->
    <script src="//cdn.jsdelivr.net/npm/medium-editor@latest/dist/js/medium-editor.min.js"></script>
    <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/medium-editor@latest/dist/css/medium-editor.min.css" type="text/css" media="screen" charset="utf-8">

    <!-- Custom CSS -->
    <link rel="stylesheet" href="{% static 'css/blog.css' %}">

    <!-- Fonts -->
    <link href="https://fonts.googleapis.com/css?family=Montserrat|Russo+One&display=swap" rel="stylesheet">

  </head>
  <body class="loader">
    <!-- Navbar -->
    <nav class="navbar custom-navbar techfont navbar-default">
      <div class="container">

        <ul class="nav navbar-nav">
          <li><a class="navbar-brand bigbrand" href="{% url 'post_list' %}">My Blog</a></li>
          <li><a href="{% url 'about' %}">About</a></li>
          <li><a href="https://www.github.com">GitHub</a></li>
          <li><a href="https://www.linkedin.com">LinkedIn</a></li>
        </ul>

        <ul class="nav navbar-nav navbar-right">

          {% if user.is_authenticated %}
            <li> <a href="{% url 'post_new' %}" >New Post</a></li>
            <li> <a href="{% url 'post_draft_list' %}" >Drafts</a></li>
            <li> <a href="{% url 'logout' %}" >Logout</a></li>
            <li> <a href="#">Welcome: {{user.username}}</a> </li>

          {% else %}
          <li> <a href="{% url 'login' %}" class="nav navbar-right"> <span class="glyphicon glyphicon-user"></span> </a> </li>

          {% endif %}
        </ul>
      </div>
    </nav>

    <!-- COntent Block -->
    <div class="content container">
      <div class="row">
        <div class="col-md-8">
          <div class="blog-posts">
            {% block content %}
            {% endblock %}
          </div>
        </div>
      </div>
    </div>

  </body>
</html>

Post_detail.html

{% extends 'blog/base.html' %}
{% block content %}

  <h1 class="posttitle loader">{{post.title}}</h1>
  {% if post.published_date %}
  <div class="date postdate">
    {{post.published_date}}
  </div>

  {% else %}
  <a href="{% url 'post_publish' pk=post.pk %}" class="btn btn-default">Publish</a>

  {% endif %}

  <p class="postcontent">{{post.text|safe|linebreaksbr}}</p>

  {% if user.is_authenticated %}
  <a href="{% url 'post_edit' pk=post.pk %}" class="btn btn-primary">
    <span class="glyphicon glyphicon-pencil"></span>
  </a>

  <a href="{% url 'post_remove' pk=post.pk %}" class="btn btn-primary">
    <span class="glyphicon glyphicon-remove"></span>
  </a>

  {% endif %}

<hr>
<a href="{% url 'add_comment_to_post' pk=post.pk %}" class="btn btn-primary btn-comment">Add Comment</a>

<div class="container">
  {% for comment in post.comments.all %}
  <br>
  {% if user.is_authenticated or comment.approved_comment %}
  {{ comment.create_date }}
    {% if not comment.approved_comment %}
      <a href="{% url 'comment_approve' pk=comment.pk %}" class="btn btn-primary">
        <span class="glyphicon glyphicon-ok"></span>
      </a>

      <a href="{% url 'comment_remove' pk=comment.pk %}" class="btn btn-default">
        <span class="glyphicon glyphicon-remove"></span>
      </a>
    {% endif %}

  <p>{{comment.text|safe|linebreaks}}</p>
  <p>Posted by: {{comment.author}}</p>

  {% endif %}
  {% empty %}
  <p>No comments!</p>

  {% endfor %}
</div>
{% endblock %}

Post_list.html(应该在运行服务器时打开的第一个页面)

{% extends 'blog/base.html' %}
{% block content %}

<div class="centerstage">
  {% for post in post_list %}
  <div class="post">
    <h1><a href="{% url 'post_detail' pk=post.pk %}">{{post.title}}</a></h1>
    <div class="date">
      <p>Published on: {{post.published_date|date:'D M Y'}}</p>
    </div>
    <a href="{% url 'post_detail' pk=post.detail %}">Comments: {{ post.approve_comments.count }}</a>
  </div>
  {% endfor %}
</div>

{% endblock %}

网址.py

博客网址

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$',views.PostListView.as_view(),name='post_list'),
    url(r'^about/$',views.AboutView.as_view(),name='about'),
    url(r'^post/(?P<pk>\d+)/$',views.PostDetailView.as_view(),name='post_detail'),
    url(r'^post/new/$',views.CreatePostView.as_view(),name='post_new'),
    url(r'^post/(?P<pk>\d+)/edit/$',views.PostUpdateView.as_view(),name='post_edit'),
    url(r'^post/(?P<pk>\d+)/remove/$',views.PostDeleteView.as_view(),name='post_remove'),
    url(r'^drafts/$',views.DraftListView.as_view(),name='post_draft_list'),
    url(r'^post/(?P<pk>\d+)/comment/$',views.add_comment_to_post,name='add_comment_to_post'),
    url(r'^comment/(?P<pk>\d+)/approve/$',views.comment_approve,name='comment_approve'),
    url(r'^comment/(?P<pk>\d+)/remove/$',views.comment_remove,name='comment_remove'),
    url(r'^post/(?P<pk>\d+)/publish/$',views.post_publish,name='post_publish'),
]

之前没有收到这个错误。突然来了。不知道出了什么问题。如何解决这个问题?

标签: djangopython-3.x

解决方案


正如它在错误消息中所说,{'pk: ''}找不到带有参数的博客网址。这基本上意味着在您正在执行的页面上"{% url 'post_detail' pk=post.pk %}"post可能是None或不是您期望的对象。您需要检查您的{% for post in post_list %}实际返回帖子。


推荐阅读