更新帖子后,我希望将用户重定向到帖子(Django-Python)

时间:2020-04-27 00:42:28

标签: python django python-3.x schema slug

我希望用户能够更新现有帖子。当用户单击“更新”,然后单击“发布”时,他们会收到一条错误消息,指出没有可直接指向的URL。但是帖子确实会在后端更新。

enter image description here

enter image description here

enter image description here

我认为return reverse('article_detail', kwargs={'slug': self.slug})行会将他们重定向回其原始帖子。

class Post(models.Model):
    title = models.CharField(max_length=100)
    content =  models.TextField()
    date_posted = models.DateTimeField(default=timezone.now())
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    url= models.SlugField(max_length=500, blank=True)
    #url= models.SlugField(max_length=500, unique=True, blank=True)

    def save(self, *args, **kwargs):
        self.url= slugify(self.title)
        super().save(*args, **kwargs)

    def __str__(self):
        return self.title 

    def get_absolute_url(self):
        return reverse('article_detail', kwargs={'slug': self.slug})

这是表架构。

sqlite> .schema blog_post
CREATE TABLE IF NOT EXISTS "blog_post" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "title" varchar(100) NOT NULL, "content" text NOT NULL, "url" varchar(500) NOT NULL, "author_id" integer NOT NULL REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED, "date_posted" datetime NOT NULL);
CREATE INDEX "blog_post_url_5af1045a" ON "blog_post" ("url");
CREATE INDEX "blog_post_author_id_dd7a8485" ON "blog_post" ("author_id");
sqlite>

views.py

class PostUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
    model = Post
    fields = ['title', 'content']

    def form_valid(self, form):
        form.instance.author = self.request.user
        return super().form_valid(form)


    def test_func(self):
        post = self.get_object()
        if self.request.user == post.author:
            return True
        return False

urls.py

urlpatterns = [
    path('', PostListView.as_view(), name='blog-home'),
    path('post/<int:pk>/', views.post_detail, name='post-detail'),
    path('post/new/', PostCreateView.as_view(), name='post-create'),
    path('post/<int:pk>/update/', PostUpdateView.as_view(), name='post-update'),
    path('post/<int:pk>/delete/', PostDeleteView.as_view(), name='post-delete'),
    path('about/', views.about, name='blog-about'),
    path('facebook/',TemplateView.as_view(template_name='blog/index.html'), name="facebook")
]

更新

我曾经有self.url= slugify(self.title),而我改成了self.url= slugify(self.id)。因为要访问我的帖子,所以您输入ID号而不是标题。不幸的是,问题仍然存在。

class Post(models.Model):
    title = models.CharField(max_length=100)
    content =  models.TextField()
    date_posted = models.DateTimeField(default=timezone.now())
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    url= models.SlugField(max_length=500, blank=True)
    #url= models.SlugField(max_length=500, unique=True, blank=True)

    def save(self, *args, **kwargs):
        self.url= slugify(self.id)
        super().save(*args, **kwargs)

enter image description here

更新2

我现在收到此错误消息。

未找到“ article_detail”的反面。 “ article_detail”不是有效的视图函数或模式名称。

enter image description here

1 个答案:

答案 0 :(得分:1)

修复get_absolute_url

def get_absolute_url(self):
    return reverse('post-detail', kwargs={'pk': self.pk})