首页 > 解决方案 > NoReverseMatch:未找到“关于”的反向。'about' 不是有效的视图函数或模式名称

问题描述

我正在 django 中构建博客并因为这个错误而卡住了。当我单击 Readmore 按钮以加载完整的博客文章时。出现此错误。它应该加载显示博客文章的详细页面,它向我显示了这个错误。我尝试了互联网上可用的不同解决方案,但没有摆脱这个错误。

在此处输入图像描述

这是我的代码!

项目 url.py:

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

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

应用网址

from django.urls import path
from . import views

urlpatterns = [
      path('', views.PostList.as_view(), name= 'home'),
      path('user/<str:username>/', views.UserPostList.as_view(), name= 'user-posts'),
      path('<slug:slug>', views.PostDetail.as_view(), name= 'post_detail'),
      path('register/', views.Register, name= 'Registration'),
      path('login/', views.Login, name= 'Login'),
      path('logout/', views.logout_view, name= 'Logout'),

]

视图.py

from django.db import models
from .forms import NewUserForm
from django.shortcuts import get_object_or_404, redirect, render
from django.http import HttpResponse
from django.contrib.auth import login,logout, authenticate
from django.contrib import messages
from django.contrib.auth.forms import  AuthenticationForm
from django.views import generic
from .models import STATUS, Post
from django.contrib.auth.models import User

class PostList(generic.ListView):
     queryset = Post.objects.filter(status=1).order_by('-created_on')
     template_name = 'Blog_App/index.html'

class UserPostList(generic.ListView):
     model = Post
     template_name = 'Blog_App/user_posts.html'
     context_object_name = 'posts'

def get_queryset(self):
    user = get_object_or_404(User, username=self.kwargs.get('username'))
    return Post.objects.filter(author=user).order_by('-created_on')
    
class PostDetail(generic.DetailView):
    model = Post
    template_name = 'Blog_App/post_detail.html'

def Register(request):
    if request.method == 'POST':
       form = NewUserForm(request.POST)
       if form.is_valid():
           user = form.save()
           login(request, user)
           messages.success(request, 'Registration succesfull')
           return redirect('home')
       messages.error(request, 'Invalid Information')
    form = NewUserForm()
    context = {'register_form': form}
    return render(request, 'Blog_App/register.html', context )

def Login(request):
   if request.method == 'POST':
      form = AuthenticationForm(request, data=request.POST)
      if form.is_valid():
        username = form.cleaned_data.get('username')
        password = form.cleaned_data.get('password')
        user = authenticate(username=username, password=password)
        if user is not None:
            login(request, user)
            messages.info(request, 'Congrats. You logged In! ')
            return redirect('home')
        else:
            messages.error(request, 'Incorrect Credentials!')
    else:
        messages.error(request, 'Invalid Information')

  form = AuthenticationForm()
  return render(request, 'Blog_App/login.html', context={'login_form': form})


def logout_view(request):
    logout(request)
    messages.info(request, 'You are succesfully Logged out')
    return redirect('home')

Base.html

<!DOCTYPE html>
<html lang="en">
{% load static %}    
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Blog-App</title>
    <link rel="stylesheet" href="{% static 'css/style.css' %}">
    <link rel="stylesheet" 
    href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
</head>
<body>
    <div class="nav-container">
      <nav class="test">
        <div class="nav-wrapper">
            <a href="#" data-target="mobile-demo" class="sidenav-trigger"><i class="material- 
                 icons">menu</i></a>
              <ul id="nav-mobile" class="hide-on-med-and-down">
                  <li><a href="{% url 'home' %}">Home</a></li>
                  <li><a href="badges.html">About </a></li>
                  <li><a href="badges.html">Contact Us</a></li>
               <ul id="nav-mobile" class="right hide-on-med-and-down">
               {% if user.is_authenticated %}
                  <li><a href="{% url 'Logout' %}">Logout</a></li>
                  <li><a href="#">{{user.username}}</a></li>
               {% else %}
                  <li><a href="{% url 'Login' %}">Login</a></li>
                  <li><a href="{% url 'Registration' %}">Register</a></li>
              {% endif %}
             </ul>
        <div>
       </nav>
          
   </div>
{% block content %}    

{% endblock content %}   


<footer class="page-footer" >
  <div class="container">
   <div class="row">
    <div class="col l6 s12">
      <h5 class="white-text">Footer Content</h5>
      <p class="grey-text text-lighten-4">You can use rows and columns here to organize your 
       footer content.</p>
    </div>
    <div class="col l4 offset-l2 s12">
      <h5 class="white-text">Links</h5>
      <ul>
        <li><a class="grey-text text-lighten-3" href="#!">Link 1</a></li>
        <li><a class="grey-text text-lighten-3" href="#!">Link 2</a></li>
        <li><a class="grey-text text-lighten-3" href="#!">Link 3</a></li>
        <li><a class="grey-text text-lighten-3" href="#!">Link 4</a></li>
      </ul>
    </div>
   </div>
  </div>
 <div class="footer-copyright">
  <div class="container">
  © 2014 Copyright Text
  <a class="grey-text text-lighten-4 right" href="#!">More Links</a>
  </div>
 </div>
</footer>

<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"> 
</script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
    {% if messages %}
       {% for msg in messages %}  
          <script>
            swal({
               "text": "{{msg}}",
            });
          </script>
       {% endfor %}
   {% endif %}
</body>
</html>

索引.html

{% extends 'base.html' %}
{% load static %}

{% block content %}
<div class="Blog-posts">
   <div class="row">
    
       <div class="div-1">
          <div class="col s12 m6 ">
             {% for post in post_list %}
                  <div class="card card-list blue-grey darken-1">
                    <div class="card-content white-text">
                       <div class="author">
                          <a href="{% url 'user-posts' post.author.username %}">
                             <h5 class="author-name"><img class="author-img" src="{% static 
                              'images/profile.jpg' %}" alt="" height="40px" width="40px">
                                {{post.author}}</h5>
                          </a>
                             <small class="post-created_on">{{post.created_on}}</small>
          
          
                          </div>
                       <span class="card-title">{{post.title}}</span>
                       <p>{{post.content|slice:"200"}}</p>
                    </div>
                    <div class="Readmore-btn">
                       <a href="{% url 'post_detail' post.slug %}" class="waves-effect waves- 
                        light btn-small">Read More</a></div>
          
                  </div>
          
             {% endfor %}
          </div>
       </div>
       <div class="div-2">
          <div class="col m5">
                <div class="card card-sidebar">
                  <div class="card-content black-text">
                    <span class="card-title">About Us</span>
                    <p>I am a very simple card. I am good at containing small bits of 
                     information.
                    I am convenient because I require little markup to use effectively.</p>
                  </div>
                  <div class="card-action">
                    <div class="Readmore-btn_about"><a href="#" class="waves-effect waves- 
                     light btn-small">Read More</a>
          
          
              </div>
       </div>
         </div>
       </div>
    </div>

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

标签: djangodjango-modelsdjango-viewsdjango-templatesdjango-urls

解决方案


如果您面临同样的问题。所以在这里我如何摆脱这个。Django 在 base.html 中给了我某种 {% url 'about' %} 模式的错误。但它实际上存在于另一个名为 post_detail.html 的 html 文件中。因此,如果您也面临其他文件的相同类型的错误检查,您会在其他 html 文件中发现错误。谢谢


推荐阅读