我想将此函数链接到基本模板中的链接,但我有这个错误,可以找到什么解决方案?我应该使用反向功能吗?
我的观点
def forumPostList(request, pk):
conversation = get_object_or_404(Conversation, pk=pk)
form_response = PostModelForm()
posts_conversation = Post.objects.filter(conversation=conversation)
context = {"conversation": conversation,
"posts_conversation": posts_conversation,
"form_response": form_response
}
return render(request, "account/forum_post.html", context)
{% extends 'base.html' %}
{% block content %}
<h1>Received messages:</h1>
<hr>
<br>
{% for post in posts_conversation %}
<h3>{{ conversation.title }}</h3>
<p>Posts: {{ post.author_post.posts.count }}</p>
<p>Posts: {{ post.author_post.username }}</p>
{% endfor %}
{% endblock content %}
我的基地
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="{% url 'forum_post' %}">forum_post</a>
</li>
forumPostList
视图需要在url中获取"pk"作为参数。因此,对于使用链接调用它,您的链接应该指定"pk">
所以你可以这样使用url块:
{% url 'forum_post' conversation_pk %}
应该以某种方式指定coversation_pk
。