【发布时间】:2019-11-24 17:48:18
【问题描述】:
我无法将 cmets 添加到我的 django Web 应用程序。我想添加 cmets 以允许用户评论帖子,就像在普通网络博客中一样。我尝试了几种方法,但对我来说没有任何效果。
经过几周的搜索并尝试了不同的方法来解决这个问题,我一直在思考这个问题。我有一些进展,但现在完全是我想要的。
现在我只能从管理面板添加“cmets”
它看起来像这样(来自管理面板)
像这样(来自用户界面)
我在填充 cmets 方面遇到了这个问题(我不太明白为什么会发生这种情况¯_(ツ)_/¯ )
无论如何,这是我的代码,希望有人知道如何解决这个问题:
models.py
class Comment(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')
author = models.ForeignKey(User, on_delete=models.CASCADE)
text = models.TextField()
created_date = models.DateField(auto_now_add=True)
def __str__(self):
return self.text
...
class Post(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
categories = models.ManyToManyField('Category', related_name='posts')
image = models.ImageField(upload_to='images/', default="images/None/no-img.jpg")
slug= models.SlugField(max_length=500, unique=True, null=True, blank=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-detail', kwargs={'slug': self.slug})
post_detail.html
<article class="media content-section">
{% for comment in post.comments.all %}
<ul>
{{ comment.text }}
{% for reply in comment.replies.all %}
<li>
{{ reply.text }}
</li>
{% endfor %}
<ul>
{% endfor %}
</article>
我还阅读了this article关于初学者的创作博客,他们有我想要的工作评论部分,但我试图将他们的代码实现到我的网络应用程序中,但对我没有任何帮助。
但是当我尝试按照他们的教程进行操作时,我只有这样:
这是这个不成功的解决方案的代码(但我觉得它正在工作,也许我做错了什么)
post_detail.html
{% extends 'blog/base.html' %}
{% block content %}
<article class="media content-section">
<img class="rounded-circle article-img" src="{{ object.author.profile.image.url }}" alt="">
<div class="article-metadata">
<a class="mr-2 author_title" href="{% url 'user-posts' object.author.username %}">@{{ object.author }}</a>
<small class="text-muted">{{ object.date_posted|date:"N d, Y" }}</small>
<div>
<!-- category section -->
<small class="text-muted">
Categories:
{% for category in post.categories.all %}
<a href="{% url 'blog_category' category.name %}">
{{ category.name }}
</a>
{% endfor %}
</small>
</div>
{% if object.author == user %}
<div>
<a class='btn btn-secondary btn-sm mt-1 mb-1' href="{% url 'post-update' object.slug %}">Update</a>
<a class='btn btn-danger btn-sm mt-1 mb-1' href="{% url 'post-delete' object.slug %}">Delete</a>
</div>
{% endif %}
</div>
</article>
<article class="media content-section">
<div class="media-body">
<img class="img-fluid center" id="rcorners3" src="{{ object.image.url }}" alt="none">
<h2 class="article-title text-center">{{ object.title }}</h2>
<p class="article-content">{{ object.content }}</p>
</div>
</article>
<article class="media content-section">
<form action="/blog/{{ post.pk }}/" method="post">
{% csrf_token %}
<div class="form-group">
{{ form.author }}
</div>
<div class="form-group">
{{ form.body }}
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<h3>Comments:</h3>
{% for comment in comments %}
<p>
On {{comment.created_on.date }}
<b>{{ comment.author }}</b> wrote:
</p>
<p>{{ comment.body }}</p>
<hr>
{% endfor %}
</article>
{% endblock content %}
views.py
...
def comment(request):
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
comment = Comment(
author=form.cleaned_data["author"],
body=form.cleaned_data["body"],
post=post
)
comment.save()
comments = Comment.objects.filter(post=post)
context = {
"post": post,
"comments": comments,
"form": form,
}
models.py
...
class Comment(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')
author = models.ForeignKey(User, on_delete=models.CASCADE)
text = models.TextField()
created_date = models.DateField(auto_now_add=True)
def __str__(self):
return self.text
forms.py
from django import forms
class CommentForm(forms.Form):
author = forms.CharField(
max_length=60,
widget=forms.TextInput(attrs={
"class": "form-control",
"placeholder": "Your Name"
})
)
body = forms.CharField(widget=forms.Textarea(
attrs={
"class": "form-control",
"placeholder": "Leave a comment!"
})
)
【问题讨论】:
-
在您的评论模型中,您有文本,但在您的评论表单中,您有正文。也许您可以先尝试修复表单,然后尝试。
-
与您的问题无关,但我注意到您的帖子中也有图像字段。您是否打算只允许一张图片用于帖子?我可以想象一个帖子可能有几张图片,您可能还希望允许用户为他们的帖子设置样式。如果是这种情况,您可能希望将 Django-ckeditor 用于您的文本区域,它允许富文本编辑、多张图片上传、表情符号等等。
-
@ha-neul 谢谢,这是我一直在寻找但找不到的工具 :) 现在我可以更轻松地制作用户友好的帖子部分了。
标签: django django-models django-forms django-templates django-views