1

I'm using Django 1.8 to create an application and I'm using django_comments application to handle commenting system. I want only authenticated users to be able to write a new comment. So I've excluded name, email and url fields in forms.py since I want them to be hidden and retrieve these info automatically from logged in users:

class CommentDetailsForm(CommentSecurityForm):
    exclude = ('name', 'email', 'url')
    comment = forms.CharField(...)

But there's a function in CommentDetailsForm class to save these info in a dictionary to show them in Admin area:

def get_comment_create_data(self):
    return dict(
        content_type=ContentType.objects.get_for_model(self.target_object),
        object_pk=force_text(self.target_object._get_pk_val()),
        user_name=self.cleaned_data["name"],
        user_email=self.cleaned_data["email"],
        user_url=self.cleaned_data["url"],
        comment=self.cleaned_data["comment"],
        submit_date=timezone.now(),
        site_id=settings.SITE_ID,
        is_public=True,
        is_removed=False,
    )

My question is: How can I fill user_name, user_email and user_url in this function automatically when a logged in user post a new comment.

I guess one solution may be put something like <input type="hidden" name="user_name" value="{{ request.user.username }}" /> in template, but I'm not sure how to do it.

sheshkovsky
  • 1,302
  • 3
  • 18
  • 41
  • 2
    Just use request.user in your view, there is no need for additional data in your template/form. – Jingo Oct 08 '15 at 08:42
  • How should I override this function fields in the views? – sheshkovsky Oct 08 '15 at 08:52
  • 1
    The user information in the comments app should be automatically set for authenticated users. For anonymous users only the user_name field is set. There are methods in the `_get_user_info` method that depend on the fields you're excluding. – Brandon Taylor Oct 08 '15 at 12:08
  • @Brandon I exclude them because I don't want them to be visible for authenticated users. Is there a way to hide those fields without excluding them? – sheshkovsky Oct 08 '15 at 12:33
  • You have a few choices: override the form class to change those field widgets to hidden inputs, or override the markup for the form, or hide the fields visually with CSS. – Brandon Taylor Oct 08 '15 at 12:48

0 Answers0