Django comes with comment Comment library using

Source: Internet
Author: User
Tags install django pip install django

The comments Library is a library of comments built into the Django framework that allows you to quickly build a comment system that your site needs.

Django1.9 version uses django_comments instead of "django.contrib.comments", direct pip install django-contrib-comments.

First, the activation step

Add the following app to Setting.py's Installed_apps
Installed_apps = (
...
' Django_comments ',
...
)

Second, how to use comments in the template

Load comments This template tag in the template file:

1 {%load comments%}

Iii. How to display comments in a template

Examples of use are:

1234 {%get_comment_listfor [object] as [comment_list]%}{%forcomment in comment_list%}    <p>on {{comment.submit_date|date:”F,j,Y”}}, {{comment.user_name}} said: {{comment.comment|safe}}</p>{%endfor%}

Iv. displaying a form for the user to add a comment

You can simply use the built-in comment form template, as in the following example:

1234 <div id = ' commentform '; / H2> { % render_ Comment_form for [ object " % < / DIV>

Just use this template tag to integrate the comments system with your project. The comments library will automatically generate a comment form for you using the built-in template file, which includes the following fields:

    1. Csrfmiddlewaretoken--django CSRF Middleware Needs
    2. content_type--
    3. Content_pk--id value
    4. timestamp--Current Time
    5. security_hash--for safety testing
    6. name--Name
    7. email--Mailbox
    8. comment--Content
    9. honeypot--prevent the machine from filling up rubbish information

Five, custom comments

We can customize the entire comment form by using the Get_comment_form template tag in the template file to get a form object that can be used in the template. A more complete example is as follows:

12345678910111213141516 {%get_comment_form for post as form%}<formaction=‘{%comment_form_target%}‘method=‘post‘>    {% csrf_token %}    {{form.object_pk}}    {{form.content_type}}    {{form.timestamp}}    {{form.security_hash}}    <p><labelfor="id_name">姓名(必填):</label><inputname="name"id="id_name"></p>    <p><labelfor="id_email">邮箱(必填):</label><inputname="email"id="id_email"></p>    <p><labelfor="id_url">网站(可选):</label><inputname="url"id="id_url"></p>    <p><labelfor="id_comment">评论(必填):</label></p>    <p><textareaid="id_comment"rows="10"cols="40"name="comment"></textarea></p>    <pstyle="display:none;"><labelfor="id_honeypot">如果你在该字段中输入任何内容,那么你的评论就会被视为垃圾评论。</label> <inputtype="text"name="honeypot"id="id_honeypot"></p>    <p><inputname="post"value="发表"type="submit"/></p>    <inputtype=‘hidden‘name=‘next‘value=‘{%url post_by_id post.id%}‘/></form>

You can add the following CSS style to it

123456 <style type="text/css">label{display:inline;float:left;width:100px;}input,textarea{width:340px;}textarea{height:80px;}input[type=submit]{width:120px;margin-left:300px;}</style>

Some explanations for the code in the example:

1. Used to generate a comment submission address.

1 <formaction=‘{%comment_form_target%}‘method=‘post‘>

2. Used to review post-commit redirects.

1 <inputtype=”hidden” name=”next” value=”{%url my_comment_was_posted%}”/>

3. When customizing the form, be sure to add the phrase {% Csrf_token%}. The other four template variables are called form. Properties to generate the values or names of those hidden fields, because we see that when using the default form, comments automatically helps us to harvest all 9 fields, so we need to complete all the fields when we customize the form, otherwise we will fail to commit.

12345 {% csrf_token %}{{form.object_pk}}{{form.content_type}}{{form.timestamp}}{{form.security_hash}}

4. A description of the Honeypot field.

This field is used to prevent the machine program from releasing spam messages. The statement in the document is: when the general machine program releases spam, all the fields in the form will be filled in, and once this field is filled in, this information will be sentenced to spam, simply say this field is used to tease the machine program, I do not know whether there is no effective practical effect.

Vi. If you need to log in to display a form for posting comments

Examples are as follows:

123456 {%if user.is_authenticated%} Code class= "XML Spaces" >    < h2 > post your comments </ H2 >     {%render_comment_form for object%} {%else%}     please < a href= "/accounts/login" > Login </ a >, or < a href= "/accounts/register" > Register </ a > before commenting on {%endif%}
Vii. Number of comments displayed
1 {%get_comment_count for [object] as [comment_count]%}

Viii. links to comments

12345 {%for comment in comment_list%}    <ahref=”{%get_comment_permalink comment%}”>        permalink for comment #{{forloop.counter}}    </a>{%end for%}

Nine, comments generated after the automatic email notification site administrator

To add an email notification system to the comment system, I implemented this: modify the source code of the Django Comments library, in python27/lib/site-packages/django/contrib/comments/views/ Add the following code to the comments.py:

1234567891011 #coding:utf-8from django.core.mail import send_mailfrom django.views.decorators.csrf import csrf_exemptif comment.is_public:send_mail(    u’博客有新评论’,    u’评论者:\n’+comment.user_name+u’\n\r评论内容:\n’+comment.comment+u’\n\r评论者邮箱:\n’+comment.user_email,    ‘[email protected]’,    [‘[email protected]’],)

The Is_public attribute of the comment is judged first, because if Akismet is used, the property of the malicious comment is false, so the site receives a malicious comment and does not send a notification message to the administrator. Note that this code block should be placed in the source of the Comment.save () after the sentence, or not get the correct is_public attribute value.

X. Customizing certain templates

If the comment form is not submitted successfully, the comments library automatically loads the default template for the comments/preview.html in its source code, reminding the user that the form item is incorrect. You can copy this template in your project (the path is guaranteed to be templates/comments/preview.html), rewrite your own reminders, and add your own design style.

Django comes with comment Comment library using

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.