We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Learn from yesterday, live for today, look to tomorrow.
Adding tagging functionality
posts
django-taggit
Tag
manager
pip install django-taggit==0.21.3
PyCharm
settings.py
INSTALLED_APPS
INSTALLED_APPS = ( # ... 'blog', 'taggit', )
models.py
TaggableManager
Post
from taggit.managers import TaggableManager class Post(models.Model): # ... tags = TaggableManager()
python manage.py makemigrations blog
python manage.py migrate
shell
python manage.py shell
id=1
post
>>> from blog.models import Post >>>> post = Post.objects.get(id=1)
tags.all()
>>> post.tags.add('aaa', 'bbb', 'ccc') >>> post.tags.all() [<Tag: aaa>, <Tag: bbb>, <Tag: ccc>]
tags.remove(
)
>>> post.tags.remove('ccc') >>> post.tags.all() [<Tag: aaa>, <Tag: bbb>]
python manage.py runserver
http://localhost:8000/admin/taggit/tag
ccc
post id=1
blog/post/list.html
post title
<p class="tags">Tags: {{ post.tags.all|join:", " }}</p>
join
{{ list|join:", " }}
blog/views.py
from taggit.models import Tag def post_list(request, tag_slug=None): object_list = Post.published.all() tag = None if tag_slug: tag = get_object_or_404(Tag, slug=tag_slug) object_list = object_list.filter(tags__in=[tag]) # ... return render(request, 'blog/post/list.html', {'page': page, 'posts': posts, 'tag': tag})
post_list
tag_slug
None
URL
get_object_or_404()
tag
render()
urls.py
url(r'^tag/(?P<tag_slug>[-\w]+)/$', views.post_list, name='post_list_by_tag'),
Django
ListView
{% include "pagination.html" with page=posts %}
{% for %}
{% if tag %} <h2>Posts tagged with "{{ tag.name }}"</h2> {% endif %}
<p class="tags">
<p class="tags"> Tags: {% for tag in post.tags.all %} <a href="{% url "blog:post_list_by_tag" tag.slug %}"> {{ tag.name }} </a> {% if not forloop.last %} , {% endif %} {% endfor %} </p>
forloop.last
True
The text was updated successfully, but these errors were encountered:
No branches or pull requests
0x01 Django
Adding tagging functionality
posts
的方法django-taggit
这个第三方应用提供了Tag
模型和manager
来简单的导入标签给任何模型pip install django-taggit==0.21.3
或者用PyCharm
的管理工具安装settings.py
中将django-taggit
添加到INSTALLED_APPS
里models.py
中添加TaggableManager
标签管理器到Post
模型中django-taggit
,打开shell
,看看标签管理器的用法python manage.py shell
id=1
的post
post
增加一些标签,也可以用tags.all()
来验证是否增加成功tags.remove(
aaa)
也可以删除指定标签python manage.py runserver
后,可以在http://localhost:8000/admin/taggit/tag
页面查看到标签列表,可以看到之前创建的3个标签,被移除的ccc
也在,只是将ccc
移除到post id=1
外post
编辑页面下面也会出现一个标签字段用于添加标签blog/post/list.html
,在post title
后添加join
这个模板过滤器{{ list|join:", " }}
用指定分隔符连接列表(这里用逗号连接)posts
blog/views.py
导入Tag
模型post_list
需要一个tag_slug
参数,默认是None
,从URL
中获取posts
,如果有tag_slug
,用get_object_or_404()
获取这个tag
,然后利用个这个标签过滤获得所需要的posts
,最后在render()
中添加tag
传送给模板URL
获取tag_slug
,所以修改urls.py
,添加Django
的内置类ListView
写的post
列表,所以需要修改blog/post/list.html
{% for %}
循环上面添加tag
所过滤出的posts
,修改<p class="tags">
forloop.last
是一个布尔值,在最后一次执行循环是被设置为True
,如果不是最后一次都会输出一个逗号,最后一次,就不会继续执行输出逗号The text was updated successfully, but these errors were encountered: