Django 如何在模板中向表单动态注入值?

Django 如何在模板中向表单动态注入值?,django,Django,我正试图在Django中构建一个类似于Reddit的评论系统,用户可以在其中回复其他评论。对于每个评论,我都会创建一个单独的回复表单: {% for comment in comments.all %} {# Displaying the coment here #} <form action="." method="POST"> {% csrf_token %} {{ comment_form.as_p }} <

我正试图在Django中构建一个类似于Reddit的评论系统,用户可以在其中回复其他评论。对于每个评论,我都会创建一个单独的回复表单:

{% for comment in comments.all %}
    {# Displaying the coment here #}
    <form action="." method="POST">
        {% csrf_token %}
        {{ comment_form.as_p }}
        <p><input type="submit" value="Comment"></p>
    </form>
{% endfor %}
{%用于comments.all%}
{#在此处显示内容}
{%csrf_令牌%}
{{comment_form.as_p}}

{%endfor%}

我将如何向每个表单中注入相应注释的ID?是否有一种方法可以在模板级别指定初始值?

您可以在post方法中传递注释id

{% for comment in comments.all %}
    {# Displaying the coment here #}
    <form action="{% url 'comment_view' comment.id %}" method="POST">
        {% csrf_token %}
        {{ comment_form.as_p }}
    <p><input type="submit" value="Comment"></p>
    </form>
{% endfor %}

该视图甚至根本不呈现模板?谢谢,它将更新。但我想展示它是如何工作的。@SadojRai我以为它是关于将上下文传递给模板,而不是指定额外的参数来查看url解析。我道歉。
path('comment-view/<int:id>', views.comment_view, name="comment_view")
def comment_view(request, id):
    print('Comment id is ', id)
    return render(request, 'your-template' }