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:
Iii. How to display comments in a template
Examples of use are:
1234 |
{
%
get_comment_list
for [
object
] as [comment_list]
%
}
{
%
for
comment
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:
- Csrfmiddlewaretoken--django CSRF Middleware Needs
- content_type--
- Content_pk--id value
- timestamp--Current Time
- security_hash--for safety testing
- name--Name
- email--Mailbox
- comment--Content
- 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%}
<
form
action
=
‘{%comment_form_target%}‘
method
=
‘post‘
>
{% csrf_token %}
{{form.object_pk}}
{{form.content_type}}
{{form.timestamp}}
{{form.security_hash}}
<
p
><
label
for
=
"id_name"
>姓名(必填):</
label
><
input
name
=
"name"
id
=
"id_name"
></
p
>
<
p
><
label
for
=
"id_email"
>邮箱(必填):</
label
><
input
name
=
"email"
id
=
"id_email"
></
p
>
<
p
><
label
for
=
"id_url"
>网站(可选):</
label
><
input
name
=
"url"
id
=
"id_url"
></
p
>
<
p
><
label
for
=
"id_comment"
>评论(必填):</
label
></
p
>
<
p
><
textarea
id
=
"id_comment"
rows
=
"10"
cols
=
"40"
name
=
"comment"
></
textarea
></
p
>
<
p
style
=
"display:none;"
><
label
for
=
"id_honeypot"
>如果你在该字段中输入任何内容,那么你的评论就会被视为垃圾评论。</
label
> <
input
type
=
"text"
name
=
"honeypot"
id
=
"id_honeypot"
></
p
>
<
p
><
input
name
=
"post"
value
=
"发表"
type
=
"submit"
/></
p
>
<
input
type
=
‘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 |
< form action = ‘{%comment_form_target%}‘ method = ‘post‘ > |
2. Used to review post-commit redirects.
1 |
< input type=”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%} < a href=”{%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-8
from django.core.mail
import send_mail
from django.views.decorators.csrf
import csrf_exempt
if 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