我正在尝试使用the threadedcomments软件包添加评论系统。我尝试向其添加软件包的代码如下,显示为“ 我的代码”。我已经阅读了threadedcomments文档和Django注释文档,但是我很难确定确切的实现方法。我的尝试包含在代码(post_create.html)中。
该产品的工作方式如下(不完全像博客):有一个书单。每本书上都有一个表格。该表格保存了一个帖子,并使其与这本书相关。当您单击某本书项目(DetailView)时,您会看到一列与该本书项目相关的帖子列表。现在,我想在每个帖子上发表评论。我真的很感谢您的帮助。
现在,当我按如下所示运行它时,出现以下错误消息: “ QuerySet”对象没有属性“ _meta”
谢谢!
我的代码:(仅相关部分)
views.py:
class BookList(ListView):
model = Book
template_name='books/books.html'
class PostForm(ModelForm):
class Meta:
model = Post
fields = ['post']
widgets = {
'post': forms.Textarea()
}
def get_context_data(self, **kwargs):
context = super(BookList, self).get_context_data(**kwargs)
context['form'] = BookList.PostForm
return context
def post(self, request, *args, **kwargs):
form = BookList.PostForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.the_book_id = request.POST['book_id']
post.save()
return render(request, self.template_name, {'form': form })
class Posts(DetailView):
model = Book
template_name='books/post_create.html'
slug_field = 'id'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
book = self.object
posts = Post.objects.filter(the_book=book)
context['posts'] = posts
return context
models.py:
class Book(models.Model):
book = models.CharField(max_length=1000, blank=False, null=False, default="1")
def __str__(self):
return self.book
class Post(models.Model):
post = models.CharField(max_length=1000, blank=False, null=False)
the_book = models.ForeignKey('Book', on_delete=models.CASCADE, default="1")
date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.post
books.html:
<ul>
{% for book in object_list %}
<div class='ui card'>
<a class="content" href='{% url 'books:posts' book.id %}'>
<div class="header">{{ book }}</div>
</a>
<div class="ui bottom attached button">
<form class='ui form' action='' method='post'> {% csrf_token %}
{{ form.as_p }}
<input type="hidden" name="book_id" value="{{ book.id }}">
<div class='postbutton'>
<input class='ui button' type="submit" value="Done" />
</div>
</form>
</div>
</div>
{% empty %}
<h5>You don't have any books!</h5>
{% endfor %}
post_create.html(名称稍后会更改...):
{% load threadedcomments_tags %}
<ul>
{% for post in posts %}
<div class='ui card'>
<a class="content">
<div class="header">{{ post }}</div>
</a>
<div class="ui bottom attached button">
{% get_comment_count for posts as comment_count %}
{% get_comment_form for posts as comment_form %}
{% render_comment_form for posts with comment.id %}
{% get_comment_list for posts as comment_list %}
{% render_comment_list for posts %}
</div>
</div>
{% empty %}
<h5>You don't have any posts!</h5>
{% endfor %}
</ul>
当我将post_create.html中的“ posts”(如“ {%get_comment_count for posts as comment_count%}”)更改为“ post”时,出现以下错误消息: “位于/ books / posts / 13的NoReverseMatch 找不到“ comments-post-comment”的相反内容。 'comments-post-comment'不是有效的视图函数或模式名称。”
(应用程序而非项目的)urls.py:
urlpatterns = [
path('', BookList.as_view(), name='books'),
path('custompost', CreateCustomBook.as_view(), name='custom_create'),
path('template', BookTemplateList.as_view(), name='template_list'),
path('posts/<int:pk>', Posts.as_view(), name='posts'),
path('posts/<int:pk>', include('django_comments.urls'))
最后两个共享相同的URL。可以肯定这是不对的,但是不确定该怎么做。
如果可以更好地使用基于功能的视图或使用自定义注释系统或其他程序包,我愿意使用。如果这样的更改会更好,那么由于我是新手,因此非常感谢您详细说明如何实现。对此,我真的非常感激。