这是django.contrib.comments.forms的表单文件:
https://github.com/django/django/blob/master/django/contrib/comments/forms.py
我需要创建一个该表单的对象并在模板中使用它。我不想在模板中手动制作 html 表单对象,我想重用那个 contrib.comments.forms。
我该怎么做?
请您参考如下方法:
是这样的。可能需要根据您希望如何处理表单数据来修改此代码。
from django.contrib.comments.forms import CommentForm
# views.py
dev my_view(request):
my_obj = MyModel.objects.get(id=1)
form = CommentForm(my_obj)
return render(request, 'comment-template.html', {'form': form})
# comment_template.html
<form action="{% comment_form_target %}" method="post">
{% csrf token %}
{{ form.as_p }}
<input type="submit">
</form>
