【问题标题】:I'm getting a NoReverseMatch error in my home page我的主页出现 NoReverseMatch 错误
【发布时间】:2021-01-17 23:23:13
【问题描述】:

我的主页出现NoReverseMatch 错误。它来自我从公告应用程序注入的 html。它说找不到链接的反向。当我删除该行时,它显示卡片但带有模板标签的文本。

_announcement_home.html:

<div class="container">
    <div class="card announcement-card" style="width: 18rem;">
      <h5 class="card-header">Announcement</h5>
      <div class="card-body">
        <h5 class="card-title">{{announcement.title}}</h5>
        <p class="card-text">{{announcement.text}}</p>
        <span class="announcement-date">{{announcement.date}}</span>
        {% if user.is_authenticated %}
          <a href="{% url 'announcement:single' pk=self.pk %}" class="btn btn-info">Change</a>
        {% endif %}
  
      </div>
    </div>
<br>


    
</div>
    

index.html:

{% extends 'base.html' %}


{% block content %}
<div class="w3-container w3-teal">
<h1>BANNER HERE</h1> 
<p>Dito yung banner</p> 
</div>

{% include 'announcement/_announcement_home.html' %}

{% endblock  %}

urls.py:

from django.urls import path
from . import views

app_name = 'announcement'

urlpatterns = [
    path('create/', views.AnnouncementCreateView.as_view(), name='create'),
    path('', views.AnnouncementListView.as_view(), name='list'),
    path('posts/<int:pk>/', views.AnnouncementDetailView.as_view(), name='single'),
    path('delete/<int:pk>/', views.AnnouncementDeleteView.as_view(), name='destroy'),
    path('edit/<int:pk>/', views.AnnouncementUpdateView.as_view(), name='edit')
]

main urls.py:

"""urcipro URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
from home import views
from django.conf import settings
from django.conf.urls.static import static


urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.Home.as_view(), name='home'),
    path('bod/', views.BOD.as_view(), name='bod'),
    path('announcement/', include('announcement.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

views.py:

from django.shortcuts import render
from django.views import generic
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy, reverse 
from django.contrib import messages
from . import forms
from . import models
# Create your views here.

class AnnouncementListView(LoginRequiredMixin, generic.ListView):
    model = models.Announcement

class AnnouncementDetailView(LoginRequiredMixin, generic.DetailView ):
    model = models.Announcement

class AnnouncementUpdateView(LoginRequiredMixin, generic.UpdateView):
    model = models.Announcement
    form_class = forms.AnnouncementForm

class AnnouncementCreateView(LoginRequiredMixin, generic.CreateView ):
    model = models.Announcement
    form_class = forms.AnnouncementForm

    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.user = self.request.user
        self.object.save()
        return super().form_valid(form)

class AnnouncementDeleteView(LoginRequiredMixin, generic.DeleteView ):
    model = models.Announcement
    
    def get_success_url(self):
        return reverse('home')

    def delete(self, *args, **kwargs):
        messages.success(self.request, "Post Deleted")
        return super().delete(*args, **kwargs)

home app views.py:

from django.shortcuts import render
from django.views.generic import TemplateView
# Create your views here.

class Home(TemplateView):
    template_name = 'index.html'

class BOD(TemplateView):
    template_name = 'bod.html'

This is what I see when I remove the a tag:

错误回溯:

【问题讨论】:

  • 我们需要查看urls.py 文件,查看整个错误回溯消息也会有很大帮助。
  • 根据显示根页面的屏幕截图,您的错误似乎来自 Home 视图,但您尚未包含此视图。

标签: django django-templates


【解决方案1】:

看起来上下文有announcement 来显示此信息,然后您在未定义的url 标记中使用了self

因此将url 参数更改为announcement.pk,我们可以假设它存在,因为这是与此块一起使用的对象。

      <div class="card-body">
        <h5 class="card-title">{{announcement.title}}</h5>
        <p class="card-text">{{announcement.text}}</p>
        <span class="announcement-date">{{announcement.date}}</span>
        {% if user.is_authenticated %}
          <a href="{% url 'announcement:single' pk=announcement.pk %}" class="btn btn-info">Change</a>
        {% endif %}
  
      </div>

【讨论】:

  • 你必须包含你的视图和 URL,至少在那时,因为不清楚后端是如何设置的。
  • @JamesDomingo 而不是模板中的announcement 将其更改为object,因此{{object.title}} 等。对象的默认上下文名称为object & 基于您的视图已共享,您没有更改默认值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-07
  • 1970-01-01
相关资源
最近更新 更多