I'm getting a NoReverseMatch error in my home page

◇◆丶佛笑我妖孽 提交于 2021-02-11 16:55:49

问题


I'm getting a NoReverseMatch error in my home page. It is from the html that I injected from my announcement app. It says that the reverse of the link is cannot be found. When I removed that line It shows the card but the text with template tag.

_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:

Error traceback:


回答1:


It looks like the context has announcement to display this information, and then you used self in the url tag which isn't defined.

So change the url param to announcement.pk which we can assume will exist because that's the object in use with this block.

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


来源:https://stackoverflow.com/questions/65767016/im-getting-a-noreversematch-error-in-my-home-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!