我的主页出现NoReverseMatch错误 [英] I'm getting a NoReverseMatch error in my home page

查看:33
本文介绍了我的主页出现NoReverseMatch错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

家庭应用的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'

这是删除标签时看到的内容:

错误回溯:

推荐答案

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

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

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

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>

这篇关于我的主页出现NoReverseMatch错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆