首页 > 解决方案 > Django:在 def post() 中返回与 PostListView 相同的数据

问题描述

有没有一种简单的方法可以让我使用 PostListView 在 post 方法中返回相同的数据?

class PostListView(ListView):
    model = Post
    template_name = 'blog/home.html'
    context_object_name = 'posts'
    ordering = ['-date_posted']
    paginate_by = 5

    def post(self, request, *args, **kwargs):
       *create like in db*
       *flash message*
       return *same data as get request*

我有一个简单的 Instagram 副本应用程序,此帖子列表视图显示所有帖子。每个帖子都有一个赞按钮。如果用户单击此按钮,它将在 db 中创建一个新的类似对象并显示一条闪烁消息。之后,我想显示与单击按钮之前相同的数据。有没有办法这样做?

标签: pythondjango

解决方案


推荐阅读