create comment programmatically from CommentForm embedded in another form

2015-05-07 Thread Martin Beroiz
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:


{% 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 }}





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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/c2133f7d-8d5b-4460-a0c9-38c1f05c6290%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: create comment programmatically from CommentForm embedded in another form

2015-05-07 Thread Martin Beroiz
I just found out the CommentForm actually requires to set target_object for 
initialization.

It should've been
commForm = CommentForm(target_object = myobj, data=request.POST)

Sorry for the noise on the list!


On Thursday, 7 May 2015 18:05:09 UTC-5, Martin Beroiz wrote:
>
> 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:
>
>  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 }}
> 
> 
>
>
>
> 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.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/869179b2-0320-480b-8dbf-cdbb9e8d9013%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.