【问题标题】:Django, comments cannot get the name of the post they are onDjango,评论无法获取他们所在帖子的名称
【发布时间】:2018-09-24 09:06:11
【问题描述】:

我在使用 Python Django 时遇到问题。它与外键和模型有关。之前有人告诉我,我的问题通常非常平庸,所以请多多包涵。我正在尝试为我正在开发的社交网络配置 cmets,并且我刚刚让 cmets 工作,有点。问题是,虽然我可以输入评论并且数据库会记录它和写它的人,但它不会记录它所在的帖子。它总是返回 null。这真的是我能说的全部内容,因为我真的不明白发生了什么。这里有许多不同的代码 sn-ps 和图像,希望能够在不需要我插话的情况下详细说明这个问题。

1: 后应用的模型 https://pastebin.com/XXNsCa5g

重要的模型是:

class Comment(models.Model):

2: 回复模板: https://pastebin.com/GsLUQqYp

3: 回复视图:

class PostReplyView(CreateView):
model = models.Comment
template_name = 'post_reply.html'
fields = ['comment']

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

最后,一个评论发布时缺少的内容的可视化表示: enter image description here

【问题讨论】:

  • 也许你需要在类的fields属性中包含post字段

标签: python django html web server


【解决方案1】:

使用普通视图更新:

models.py

from django.db import models
from user.models import MyUser
from django.urls import reverse
# Create your models here.

class Post(models.Model):
    user = models.ForeignKey(MyUser, on_delete=models.CASCADE)
    title = models.CharField(max_length=255)
    content = models.TextField()
    posted_on = models.DateTimeField(auto_now=True, auto_now_add=False)
    updated_on = models.DateTimeField(auto_now=False, auto_now_add=True)
    slug = models.SlugField(blank=True, null=True)

    def __str__(self):
        return "%s by %s" % (self.title, self.user.get_full_name())

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

class Comment(models.Model):
    author = models.ForeignKey(MyUser, on_delete=models.CASCADE)
    post = models.ForeignKey(Post, on_delete=models.CASCADE)
    comment = models.TextField()
    posted_on = models.DateTimeField(auto_now_add=True, auto_now=False)

    def __str__(self):
        return "%s commented on %s" % (self.author, self.post)

forms.py

from django import forms
from .models import Post, Comment

class newPost(forms.ModelForm):
    class Meta:
        model = Post
        fields = ['title','content']


class newComment(forms.ModelForm):
    class Meta:
        model = Comment
        fields = ['comment']

views.py

def detail(request, slug=None):
    post = get_object_or_404(Post, slug=slug)
    user = request.user

    if request.method == 'POST':
        comment = newComment(request.POST)
        if comment.is_valid():
            new_Comment = comment.save(commit=False)
            new_Comment.author = user
            new_Comment.post = post
            new_Comment.save()
            # successfully commented, now do whatever you want
            return HttpResponse('commented')
    else:
        comment = newComment()

    context = {
        'post' : post,
        'comment' : comment
    }
    return render(request, 'post/detail.html', context)

detail.html

<h1>{{ post.title }}</h1>
<p>{{ post.content }}</p>

<hr>

<form action="" method="post">
    {% csrf_token %}
    {{ comment.as_p }}
    <input type="submit">
</form>

** 你在“post”上得到空白字段,因为你没有将它与任何帖子链接,但在上面的例子中,我已经向你展示了我是如何做到的,以及你如何也可以为你的项目做同样的事情 * *

【讨论】:

  • 我很抱歉,但我不太确定我应该如何处理您在这里向我展示的内容。我需要在 models.py 文件中具体替换什么,用什么替换?
  • 1) 您需要了解“评论模型”中有哪些字段以及其中有多少是“Foreign_key”。 2)你得到错误的唯一部分是因为在views.py中,如果你仔细观察,那么你正在链接你的一个foreign_key(作者到登录的用户),但是因为你在你需要的模型中还有另一个foreign_key也可以将该foreign_key/field 链接到现有帖子。 3)您正在使用基于类的视图,我将更新我的答案,请检查一下,它将涵盖如何将您的评论与相应的帖子链接
猜你喜欢
  • 2018-12-23
  • 1970-01-01
  • 2015-10-14
  • 1970-01-01
  • 1970-01-01
  • 2012-04-12
  • 2018-08-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多