首页 > 解决方案 > 为什么我的文字帖子无法保存,它以前工作过?

问题描述

我的文本帖子在添加通知功能之前可以正常工作,但现在不行了,我有一个调试打印但没有看到帖子对象,而是看到了发布它的用户。除了获取帖子 ID 和通知功能外,我没有更改任何内容 timesince,我对图片和视频帖子使用了完全相同的方法,它们工作正常,所以对我来说没有意义。我在通知功能中有一个调试打印,以确保帖子类型和 id 已正确传递,并且确实显示了 id 和类型

视图.py

@login_required()
def text_post(request):
    if request.method == "POST":
        form = TextPostForm(request.POST)
        if form.is_valid():
            t_post = form.save(commit=False)
            t_post.author = request.user
            t_post.save()

            id = t_post.id
            time = timesince
            print(t_post) # t_post shows the user instead of the post

            followers = list(request.user.followers.all())
            notify(followers,
            request.user,
            f"{request.user.username} posted {time}",
            id,
            'text'
            )
            print(t_post)
            #redirect to last page
            return redirect('home')
    else:
        form = TextPostForm()
        post_type = 'text'
        return render(request, 'create_post.html', {'form': form, 'post_type': post_type})

表格.py

class TextPostForm(forms.ModelForm):
    class Meta:
        model = TextPost
        fields = ('description','privacy', 'categories')

标签: djangodjango-views

解决方案


推荐阅读