Python NoReverseMatch at/add_post

Python NoReverseMatch at/add_post,python,django,Python,Django,我一直在使用django在一个博客网站上工作,我在主页中添加了一个帖子,而没有进入管理页面,但是当我使用新的方式发布帖子时,我得到了这个错误 这是我的models.py文件 from django.db import models from django.contrib.auth.models import User from django.urls import reverse class Post(models.Model): title = models.CharFi

我一直在使用django在一个博客网站上工作,我在主页中添加了一个帖子,而没有进入管理页面,但是当我使用新的方式发布帖子时,我得到了这个错误

这是我的models.py文件

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


class Post(models.Model):
    title = models.CharField(max_length=255)
    title_tag = models.CharField(max_length=255)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    body = models.TextField(max_length=3500)

    def __str__(self):
        return (self.title + " | " + str(self.author))

    def get_absolute_url(self):
        return reverse("article-view", args=(str(self.id)))
这是views.py文件

from django.views.generic import ListView, DetailView, CreateView
from .models import Post


class HomeView(ListView):
    model = Post
    template_name = "home.html"


class ArticleDetailView(DetailView):
    model = Post
    template_name = "detail_view.html"


class AddPostView(CreateView):
    model = Post
    template_name = "add_post.html"
    fields = "__all__"
这是polls/url.py

from django.urls import path

from .views import HomeView, ArticleDetailView, AddPostView

urlpatterns = [
    path('', HomeView.as_view(), name='home'),
    path('article/<int:pk>', ArticleDetailView.as_view(), name='article-view'),
    path('add_post/', AddPostView.as_view(), name='add_post'),
]
从django.url导入路径
从.views导入HomeView、ArticleDetailView、AddPostView
URL模式=[
路径(“”,HomeView.as_view(),name='home'),
路径('article/',articletailview.as_view(),name='article-view'),
路径('add_post/',AddPostView.as_view(),name='add_post'),
]
这是add_post.html文件

{% extends 'base.html' %}

{% block content %}
<head>
    <title>Adding Post</title>
</head>

<h1>Add Blog Posts</h1>

<form method="POST">
    {% csrf_token %}
    {{ form.as_p }}
    <button class="btn btn-secondary">Post</button>
</form>

{% endblock %}
{%extends'base.html%}
{%block content%}
新增职位
添加博客帖子
{%csrf_令牌%}
{{form.as_p}}
邮递
{%endblock%}

谢谢。

好的,看来这是由模型的
获取绝对url
反向
args=()
引起的。我将
models.py
中的以下代码从:

def get_absolute_url(self):
        return reverse("article-view", args=(str(self.id)))
进入


问题似乎是
args=()
,它正在迭代
str(self.id)
。因此
id=10
实际上将作为
元组(1,0)
返回。我还删除了
self.id
周围的
str()
,因为URL带有
int

你可以尝试替换
路径('add_post',AddPostView.as_view(),name='add_post'),
路径('add_post/',AddPostView.as_view(),name='add_post'),
有一个错误消息的屏幕截图,感谢您的帮助这里有一些文件我没有显示home.html文件和detail\u view.html文件我应该在itOkay中显示代码吗?答案现在应该有帮助了
def get_absolute_url(self):
        return reverse("article-view", args=[self.id])