Django集成第三方标签功能

django-taggit模块是一个可重用的应用程序,它主要提供一个标签模型和一个管理器,可以轻松地向任意模型添加标签。

https://github.com/alex/django-taggit

目录

安装django-taggit

添加taggit到setting.py中的INSTALLED_APPS

编辑models.py,将标签应用到post模型

更新数据库

重构blog应用程序的views.py文件

添加标签路径到urls.py

更新list.html模版,为标签增加链接


安装django-taggit

bash 复制代码
python -m pip install django-taggit

添加taggit到setting.py中的INSTALLED_APPS

python 复制代码
INSTALL_APPS = [
	#...
	'taggit',
]

编辑models.py,将标签应用到post模型

python 复制代码
from taggit.managers import TaggableManager

class Post(models.Mocel):
	#...
	tags = TaggableManager()

标记管理器将允许您从Post对象中添加、检索和删除标记。

更新数据库

python 复制代码
python manage.py makemigrations blog

python manage.py migrate

重构blog应用程序的views.py文件

导入标签模型表单django-taggit,并将post_list视图更改为根据标签筛选帖子。视图最终是这样的。

python 复制代码
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])
    paginator = Paginator(object_list,3)
    page = request.GET.get('page')
    try:
        posts = paginator.page(page)
    except PageNotAnInteger:
        posts = paginator.page(1)
    except EmptyPage:
        posts = paginator.page(paginator.num_pages)
  
    template = "blog/post/list.html"
    context = {
        "page":page,
        "posts":posts,
        "tag":tag,
    }

    return render(request,template,context)
  • 视图接受一个可选的tag_slug参数,默认值为None。该参数将出现在URL中。
  • 在视图中,构建初始QuerySet,检索所有已发布的文章,如果存在给定的标记段,我们使用get_object_or_404()获取带有给定段的tag对象。
  • 然后,根据包含给定标记的帖子筛选帖子列表。由于这是一个多对多关系,我们必须根据给定列表中包含的标记进行筛选,在示例中,该列表只包含一个元素。

添加标签路径到urls.py

python 复制代码
path("", views.post_list, name="post_list"),
path('tag/<slug:tag_slug>/',views.post_list, name='post_list_by_tag'),

两个路径都指向相同的视图,但是我们对它们的命名不同。

第一种模式将调用不带任何可选参数的post_list视图,而第二种模式将调用带tag_slug参数的视图。这里使用一个slug路径转换器将参数匹配为一个小写字符串,其中包含ASCII字母或数字,以及连字符和下划线字符。

更新list.html模版,为标签增加链接

html 复制代码
    {% if tag %}
        <h2>Posts tagged with "{{ tag.name }}"</h2>
    {% endif %}

如果tag不是None,显示正在过滤的tag名称。

html 复制代码
        <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>

遍历一个帖子的所有标记,显示一个自定义链接到URL,以根据该标记过滤帖子。我们使用{% URL "blog:post_list_by_tag"标签构建URL。使用URL的名称和slug标记作为其参数。我们用逗号分隔标签。

相关推荐
IVEN_11 小时前
只会Python皮毛?深入理解这几点,轻松进阶全栈开发
python·全栈
Ray Liang12 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
AI攻城狮13 小时前
如何给 AI Agent 做"断舍离":OpenClaw Session 自动清理实践
python
千寻girling13 小时前
一份不可多得的 《 Python 》语言教程
人工智能·后端·python
AI攻城狮16 小时前
用 Playwright 实现博客一键发布到稀土掘金
python·自动化运维
曲幽16 小时前
FastAPI分布式系统实战:拆解分布式系统中常见问题及解决方案
redis·python·fastapi·web·httpx·lock·asyncio
孟健1 天前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞1 天前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽1 天前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers