添加了空白帖子以及用户创建的帖子(Django)

时间:2020-06-11 07:54:20

标签: python django python-3.x django-models django-views

只要您在我的论坛页面上点击添加帖子按钮,它将立即在论坛中添加空白帖子,同时仍然添加用户创建的帖子。

在点击添加帖子之前:

enter image description here

点击添加帖子后(但未添加任何帖子详细信息):

enter image description here

我可以更改创建帖子时的视图,但是我结束了完全删除添加帖子的功能!因此,想看看是否有人对为什么要添加空白帖子以及用户创建的帖子有所了解吗?

用于添加帖子的视图:

@login_required
def create_forum_post(request, pk=None):
    """ Create a view that allows us to create a post """

    post = get_object_or_404(Post, pk=pk) if pk else None
    if not post:
        post = Post.objects.create(creator_id=request.user)
    if request.method == "POST":
        form = ForumPostForm(request.POST, request.FILES, instance=post)
        if form.is_valid():
            post = form.save()
        return redirect('forum_post_details', post.pk)
    else:
        form = ForumPostForm(instance=post)
    return render(request, 'forum_post_form.html', {'form': form})

发布模型:

class Post(models.Model):
    creator_id = models.ForeignKey(User, null=False, default=1)
    title = models.CharField(max_length=200)
    content = models.TextField()
    created_date = models.DateTimeField(auto_now_add=True)
    published_date = models.DateTimeField(blank=True, null=True, default=timezone.now)
    views = models.IntegerField(default=0)
    category = models.CharField(choices=CATEGORY_CHOICES, max_length=30, blank=True, null=True)
    image = models.ImageField(upload_to="img", blank=True, null=True)

    def __str__(self):
        return self.title

0 个答案:

没有答案