Authenticated comment in Django 1.1?

(Now that Django 1.1 is in candidate status on the release, this may be the right time to ask about it.)

I have searched everywhere for ways to extend the Django comment application to support authenticated comments. After reading the comment model several times, I found that ForeignKey before User already exists.

From django.contrib.comments.models :

 class Comment(BaseCommentAbstractModel): """ A user comment about some object. """ # Who posted this comment? If ``user`` is set then it was an authenticated # user; otherwise at least user_name should have been set and the comment # was posted by a non-authenticated user. user = models.ForeignKey(User, verbose_name=_('user'), blank=True, null=True, related_name="%(class)s_comments") user_name = models.CharField(_("user name"), max_length=50, blank=True) user_email = models.EmailField(_("user email address"), blank=True) user_url = models.URLField(_("user URL"), blank=True) 

It seems like I can't come up with a User setting. If I use comments as is, even if I'm authenticated, it still seems to need other fields. I assume that I should redefine the form and make it there? In addition, if I use User , I should ignore the fact that user_name , user_email and user_url will be empty and just extract this information from the corresponding profile model, fix it?

Although the answers can be quite trivial at the end, I am just surprised that it was not written or even spoken.

+6
python comments django
source share
6 answers
+1
source share

WordPress and other systems do this without a problem. If you are logged in, the comment form should simply β€œdo the right thing” and remove the name / email / url fields. Does this not mean what exactly such a heavy lifting of the frame should do for you?

Instead of dancing with subclass models for something that should be trivially easy, I find it easier to create the form manually in the template and provide hidden values ​​for the fields that it needs. This works great for sites that accept only comments from authenticated users:

 {% if user.is_authenticated %} {% get_comment_form for [object] as form %} <form action="{% comment_form_target %}" method="POST"> {% csrf_token %} {{ form.comment }} {{ form.honeypot }} {{ form.content_type }} {{ form.object_pk }} {{ form.timestamp }} {{ form.security_hash }} <input type="hidden" name="next" value="{% url [the_view] [object].id %}" /> <input type="submit" value="Add comment" id="id_submit" /> </form> {% else %} <p>Please <a href="{% url auth_login %}">log in</a> to leave a comment.</p> {% endif %} 

Note that this will leave the honeypot field visible; you'll want to hide it in your CSS:

 #id_honeypot { visibility:hidden; } 

If you want to enable comments for anonymous or authenticated users, replace the auth_login line above with a standard comment form call.

+4
source share

I recommend that if you have a question about the internal components of Django, you should look at the source.

If we look at the beginning of the post_comment view , we see that the POST request request has been copied, and the email address and username are pasted. They are still required (as shown in the form source ), so this data must either be on the form or the user must provide it.

To answer your Superjoe question, the view attaches the user to the comment before saving it (as seen near the end of the post_comment view ).

+3
source share

Use the profile model for additional credentials besides username and password. You can call user.get_profile () if you include this line in your profile:

 user = models.ForeignKey(User, unique=True) 

and this line in settings.py:

 AUTH_PROFILE_MODULE = 'yourapp.Profile' 
+1
source share

Firstly, the comment application already supports both authenticated and anonymous users, so I assume that you want to accept comments only from authenticated users?

Thejaswi Puthraya had a series of articles on his blog about this. Basically, it pre-populates the name and email fields in the comment form and replaces them with hidden fields, and then defines the shell view around post_comment to ensure that the user's posting is the same as the user's logged in system, by the way. It seemed pretty simple, although perhaps a little tedious.

His blog doesn't seem to be working right now ... hope it is only temporary.

+1
source share

According to the comment, this is either - or: other fields are intended for use when user not installed. Have you verified that the corresponding columns are definitely not NULL? They are denoted as blank=True , which usually denotes required=False at the field level. If you really tried, what errors do you get?

0
source share

All Articles