我想实现fb之类的功能,就像在首页中相应帖子下方显示所有相应注释一样
model.py
from django.db import models
from django.contrib.auth.models import User
class Post(models.Model):
post = models.CharField(max_length=500)
image = models.ImageField(upload_to = 'profile_image')
user = models.ForeignKey(User ,on_delete = 'models.CASCADE')
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
def __str__(self):
return self.post
class Comment(models.Model):
user = models.ForeignKey(User,on_delete = 'models.CASCADE')
post = models.ForeignKey(Post, null = True, on_delete = 'models.CASCADE')
comment = models.TextField()
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
def __str__(self):
return self.comment
views.py
from django.views.generic import TemplateView
from django.shortcuts import render, redirect,get_object_or_404
from django.contrib.auth.models import User
from home.forms import HomeForm,Home,CommentForm
from home.models import Post ,ImagePost,Comment
class HomeView(TemplateView):
template_name = 'home/home.html'
def get(self, request):
form = HomeForm()
form1 = CommentForm()
posts = Post.objects.filter(user = request.user).order_by('-created')
comments = Comment.objects.all()
users = User.objects.exclude(id=request.user.id)
args = {
'form': form, 'posts': posts, 'users': users, 'form1':form1,
'comments':comments,
}
return render(request, self.template_name, args)
def post(self, request):
form1 = CommentForm()
text = ''
if request.method == 'POST':
form = HomeForm(request.POST,request.FILES)
if form.is_valid():
post = form.save(commit=False)
post.user = request.user
post.save()
text = form.cleaned_data['post']
form = HomeForm()
form1 = CommentForm()
return redirect('home:home')
def cmnt(self , request):
text = ''
form1 = CommentForm()
if request.method == 'POST':
form1 = CommentForm(request.POST,post.id)
if form.is_valid():
comment = form.save(commit = False)
comment.user = request.user
comment.post = request.post
comment.save()
text = form.cleaned_data['comment']
form1 = CommentForm()
form = HomeForm()
return redirect('home:home')
args = {'form': form, 'text': text , 'form1':form1}
return render(request, self.template_name, args)
home.html
{% extends 'base.html' %}
{% load static %}
{% block body %}
<div class="container">
<div class="col-md-8">
<h2>Home</h2>
<form method="POST" enctype="multipart/form-data" type = 'file'>
{% csrf_token %}
{{ form.post }}
{{ form.image }}
<br>
<button type="submit">Submit</button>
</form>
<h2>{{ text }}</h2>
{% for post in posts %}
<h1>{{ post.post }}</h1>
<br>
<img src="{{ post.image.url }}" width = 240 >
<p>Posted by {{ post.user.get_full_name }} on {{ post.created }}</p>
<form method="POST" enctype="multipart/form-data" type = 'file'>
{% csrf_token %}
{{ form1.comment }}
<br>
<button type="submit">Submit</button>
</form>
{% for comment in comments %}
{% if comment.post_id == post.id %}
<h1>{{ comment.comment }}</h1>
<p>commented by {{ comment.user.get_full_name }} on {{ comment.created }}</p>
{% endif %}
{% endfor %}
{% endfor %}
</div>
</div>
{% endblock %}
using my django admin i am able to assign comments to the respective post and display it
我发现分配带有帖子ID的评论的post_id很困难
urls.py
from django.conf.urls import url
from django.urls import path
from .import views
from home.views import HomeView
app_name = "home"
urlpatterns = [
path('',HomeView.as_view(), name='home'),
]
答案 0 :(得分:0)
首先,像这样更改评论模型:
class Comment(models.Model):
user = models.ForeignKey(User,on_delete = 'models.CASCADE')
post = models.ForeignKey(Post, null = True, on_delete = 'models.CASCADE')
text = models.TextField()
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
def __str__(self):
return self.text
第二秒,您不需要返回模板注释列表。在您的模板中这样做:
{% extends 'base.html' %}
{% load static %}
{% block body %}
<div class="container">
<div class="col-md-8">
<h2>{{ text }}</h2>
{% for post in posts %}
<h1>{{ post.post }}</h1>
<br>
<img src="{{ post.image.url }}" width = 240 >
<p>Posted by {{ post.user.get_full_name }} on {{ post.created }}</p>
{% for comment in post.comment_set.all %}
<h1>{{ comment.text }}</h1>
<p>commented by {{ comment.user.get_full_name }} on {{ comment.created }}</p>
{% endfor %}
{% endfor %}
</div>
答案 1 :(得分:0)
我认为您的问题在这里
form1 = CommentForm(request.POST,Post.id)
帖子中的p应该小写
form1 = CommentForm(request.POST,post.id)
您有两个POST表单。为每个帖子写一个单独的视图。您只在一个视图中接受两种形式。这是不正确的。
答案 2 :(得分:0)
def post(self, request):
form1 = CommentForm()
text = ''
if request.method == 'POST':
form = HomeForm(request.POST,request.FILES)
if form.is_valid():
post = form.save(commit=False)
post.user = request.user
post.save()
text = form.cleaned_data['post']
form = HomeForm()
form1 = CommentForm()
return redirect('home:home')
在发布方法中,您重定向到主页。这意味着您调用HomeView的get方法。
在get方法中,您需要上下文dict中的下面的变量
args = {
'form': form, 'posts': posts, 'users': users, 'form1':form1,
'comments':comments,
}
但是您没有为这些变量设置值。