Django 简明教程
Django - Comments
在开始之前,请注意 Django Comments 框架在 1.5 版本后已弃用。现在,你可以使用外部功能执行此操作,但如果你仍然想使用它,它仍包含在 1.6 和 1.7 版本中。从 1.8 版本开始,它已不存在,但你仍然可以在其他 GitHub 帐户上获取代码。
Before starting, note that the Django Comments framework is deprecated, since the 1.5 version. Now you can use external feature for doing so, but if you still want to use it, it’s still included in version 1.6 and 1.7. Starting version 1.8 it’s absent but you can still get the code on a different GitHub account.
注释框架能让你很轻松地为应用程序中的任何模型添加注释。
The comments framework makes it easy to attach comments to any model in your app.
若要开始使用 Django 注释框架 −
To start using the Django comments framework −
编辑项目 settings.py 文件,并在 INSTALLED_APPS 选项中添加 'django.contrib.sites' 和 'django.contrib.comments' −
Edit the project settings.py file and add 'django.contrib.sites', and 'django.contrib.comments', to INSTALLED_APPS option −
INSTALLED_APPS += ('django.contrib.sites', 'django.contrib.comments',)
获取网站 ID −
Get the site id −
>>> from django.contrib.sites.models import Site
>>> Site().save()
>>> Site.objects.all()[0].id
u'56194498e13823167dd43c64'
设置你在 settings.py 文件中获取的 ID −
Set the id you get in the settings.py file −
SITE_ID = u'56194498e13823167dd43c64'
同步 db,以创建所有注释表或集合 −
Sync db, to create all the comments table or collection −
python manage.py syncdb
将注释应用程序的 URL 添加到项目的 urls.py 中 −
Add the comment app’s URLs to your project’s urls.py −
from django.conf.urls import include
url(r'^comments/', include('django.contrib.comments.urls')),
现在我们已安装该框架,让我们更改我们的 hello 模板,以便在 Dreamreal 模型上跟踪评论。我们将列出 Dreamreal 特定条目的评论,并将该条目作为参数传递给 /myapp/hello URL。
Now that we have the framework installed, let’s change our hello templates to tracks comments on our Dreamreal model. We will list, save comments for a specific Dreamreal entry whose name will be passed as parameter to the /myapp/hello URL.
Dreamreal Model
class Dreamreal(models.Model):
website = models.CharField(max_length = 50)
mail = models.CharField(max_length = 50)
name = models.CharField(max_length = 50)
phonenumber = models.IntegerField()
class Meta:
db_table = "dreamreal"
hello view
def hello(request, Name):
today = datetime.datetime.now().date()
daysOfWeek = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
dreamreal = Dreamreal.objects.get(name = Name)
return render(request, 'hello.html', locals())
hello.html template
{% extends "main_template.html" %}
{% load comments %}
{% block title %}My Hello Page{% endblock %}
{% block content %}
<p>
Our Dreamreal Entry:
<p><strong>Name :</strong> {{dreamreal.name}}</p>
<p><strong>Website :</strong> {{dreamreal.website}}</p>
<p><strong>Phone :</strong> {{dreamreal.phonenumber}}</p>
<p><strong>Number of comments :<strong>
{% get_comment_count for dreamreal as comment_count %} {{ comment_count }}</p>
<p>List of comments :</p>
{% render_comment_list for dreamreal %}
</p>
{% render_comment_form for dreamreal %}
{% endblock %}
最后,将 URL 映射到我们的 hello 视图 −
Finally the mapping URL to our hello view −
url(r'^hello/(?P<Name>\w+)/', 'hello', name = 'hello'),
现在,
Now,
-
In our template (hello.html), load the comments framework with − {% load comments %}
-
We get the number of comments for the Dreamreal object pass by the view − {% get_comment_count for dreamreal as comment_count %}
-
We get the list of comments for the objects − {% render_comment_list for dreamreal %}
-
We display the default comments form − {% render_comment_form for dreamreal %}
当访问 /myapp/hello/steve 时,你将获取名称为 Steve 的 Dreamreal 条目的评论信息。访问该 URL,你将获取 −
When accessing /myapp/hello/steve you will get the comments info for the Dreamreal entry whose name is Steve. Accessing that URL will get you −
在发布评论后,你将被重定向至以下页面 −
On posting a comment, you will get redirected to the following page −
如果你再次访问 /myapp/hello/steve,你将看到以下页面 −
If you go to /myapp/hello/steve again, you will get to see the following page −
如你所见,现在评论数量为 1,并且你在评论列表行下方看到了评论。
As you can see, the number of comments is 1 now and you have the comment under the list of comments line.