Hello,
I'm rather new to Django and I'm trying to include django_comments form in another form a user can choose.
Basically, the user will fill a custom form (of mine) and possibly leave a comment with a form generated with django_comments.
In a nutshell, I want to save a comment programmatically from my custom view/form.
A minimal version of my 'rank' view and template is:
from django_comments.forms import CommentForm
@login_required
def rank(request):
if request.method == "POST":
rankForm = RankingForm(data=request.POST)
commForm = CommentForm(data=request.POST)
if form.is_valid() and commForm.is_valid():
r = rankForm.save()
commForm.save()
else:
rankForm = RankingForm()
return render(request, 'rank.html', {'form': rankForm, 'tc_id': '1', ... other stuff })
and in the template:
<form id="rankForm" method="post" action="{% url 'rank' %}" enctype="multipart/form-data">
{% csrf_token %}
{{ rankForm.as_p }}
{% get_comment_form for my_app.my_model tc_id as comment_form %}
{{ comment_form.comment }}
{{ comment_form.honeypot }}
{{ comment_form.content_type }}
{{ comment_form.object_pk }}
{{ comment_form.timestamp }}
{{ comment_form.security_hash }}
<input type="submit" name="submit" value="Rank Transient" />
</form>
Django throws an error on this line: commForm = CommentForm(data=request.POST)
TypeError __init__() takes at least 2 arguments (2 given)
I don't know how to initialize a comment object with the data supplied on the form and save it using the provided methods from django_comments.
I did in the past create a whole Comment() instance from scratch and filled it all with the correct data, but it seemed to me like a hack, bypassing all the security checks built-in in the comment package.
How would I go about to save a comment programmatically from my custom view/form?
Thanks,
Martin.