Python Django,评论无法获取其所在帖子的名称
我对Python Django有意见。它与外键和模型有关。以前有人告诉我,我的问题通常都非常平庸,所以请容忍我的问题。我正在尝试为我正在使用的社交网络配置评论,我刚刚收到了一些评论。问题是,虽然我可以输入一条评论,数据库会记录它和写它的人,但它不会记录它所在的帖子。它总是返回null。这是我能说的全部内容,因为我真的不明白发生了什么。下面是一些不同的代码片段和图像,希望它们能够在不需要我感叹的情况下详细阐述这个问题 1: post应用程序的模型 重要的模式是:Python Django,评论无法获取其所在帖子的名称,python,django,html,web,server,Python,Django,Html,Web,Server,我对Python Django有意见。它与外键和模型有关。以前有人告诉我,我的问题通常都非常平庸,所以请容忍我的问题。我正在尝试为我正在使用的社交网络配置评论,我刚刚收到了一些评论。问题是,虽然我可以输入一条评论,数据库会记录它和写它的人,但它不会记录它所在的帖子。它总是返回null。这是我能说的全部内容,因为我真的不明白发生了什么。下面是一些不同的代码片段和图像,希望它们能够在不需要我感叹的情况下详细阐述这个问题 1: post应用程序的模型 重要的模式是: class Comment(mo
class Comment(models.Model):
2:
答复模板:
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)
最后,在发布评论时,对其缺少的内容进行可视化表示:
使用普通视图更新: 型号.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)
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']
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)
forms.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)
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']
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)
视图.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)
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']
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.title}
{{post.content}}
{%csrf_令牌%}
{{comment.as_p}}
**“post”上的字段为空,因为您没有将其与任何post链接,但在上面的示例中,我已经向您展示了我是如何做到这一点的,以及您如何为您的项目做到这一点**也许您需要将post字段包括在classI的fields属性中,但我真的不太确定我该怎么处理你给我看的东西。我需要在models.py文件中具体替换什么?1)您需要了解“注释模型”中有哪些字段,以及其中有多少字段是“外键”。2) 出现错误的唯一部分是因为inside views.py如果仔细查看,则会链接一个外键(作者到登录用户),但由于模型中还有另一个外键,因此也需要将该外键/字段链接到现有帖子。3) 您使用的是基于类的视图,我将更新我的答案,请检查,它将涵盖如何将您的评论链接到相应的帖子