Почему django показывает NoReverseMatch в /articles/3/ Reverse for 'article_edit' with arguments '('',)' not found. Испробовано 1 шаблон(ы): []

Когда я пытаюсь связать редактирование (article_edit.html) и удаление (article_update.html) в article_detail.html, Django выдает эту ошибку.

NoReverseMatch at /articles/3/
Reverse for 'article_edit' with arguments '('',)' not found. 1 pattern(s) tried: ['articles/(?P<pk>[0-9]+)/edit/$']
Request Method: GET
Request URL:    http://127.0.0.1:8000/articles/3/
Django Version: 3.2.7
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'article_edit' with arguments '('',)' not found. 1 pattern(s) tried: ['articles/(?P<pk>[0-9]+)/edit/$']

Я пробовал без связывания этих тегов (Edit и Delete), приложение работает абсолютно нормально, но когда я добавляю эти строки кода, приложение перестает работать. вот мой шаблон article_detail.html

{% extends 'base.html' %} {% block content %}
<div class="article-entry">
  <h2>{{object.title}}</h2>
  <p>by {{object.author}} | {{object.date}}</p>
  <p>{{object.body}}</p>
</div>
<p>
  <a href="{% url 'article_edit' article.pk %}">Edit</a> |
  <a href="{% url 'article_delete' article.pk %}">Delete</a>
</p>
<p>back to <a href="{% url 'article_list' %}"></a>All Articles</p>

{% endblock content %}

вот models.py, urls.py и views.py для приложения 'articles'

from django.db import models
from django.contrib.auth import get_user_model
from django.urls import reverse


class Articles(models.Model):
    title = models.CharField(max_length=250)
    body = models.TextField()
    date = models.DateTimeField(auto_now_add=True)
    author = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse("article_detail", args=[str(self.id)])
from django.urls import reverse_lazy
from django.urls.base import reverse
from django.views.generic import ListView, DetailView
from django.views.generic.edit import UpdateView, DeleteView

from .models import Articles


class ArticleListView(ListView):
    model = Articles
    template_name = "article_list.html"


class ArticleDetailView(DetailView):
    model = Articles
    template_name = "article_detail.html"


class ArticleUpdateView(UpdateView):
    model = Articles
    fields = (
        "title",
        "body",
    )
    template_name = "article_edit.html"


class ArticleDeleteView(DeleteView):
    model = Articles
    template_name = "article_delete.html"
    success_url = reverse_lazy("article_list"
from django.conf.urls import url
from django.urls import path
from .views import (
    ArticleListView,
    ArticleUpdateView,
    ArticleDeleteView,
    ArticleDetailView,
)


urlpatterns = [
    path("", ArticleListView.as_view(), name="article_list"),
    path("<int:pk>/edit/", ArticleUpdateView.as_view(), name="article_edit"),
    path("<int:pk>/", ArticleDetailView.as_view(), name="article_detail"),
    path("<int:pk>/delete/", ArticleDeleteView.as_view(), name="article_delete"),
]

Это потому, что в вашем шаблоне вы пытаетесь получить доступ к несуществующему пк. Попробуйте изменить "article.pk" на "object.id".

Это потому, что в вашем шаблоне вы пытаетесь получить доступ к несуществующему пк. Попробуйте изменить "article.pk" на "object.id".

Вернуться на верх