V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
pc10201
V2EX  ›  Python

django 如何在 sidebar 侧边栏显示随机文章?

  •  
  •   pc10201 · 2015-10-07 10:31:07 +08:00 · 2656 次点击
    这是一个创建于 3133 天前的主题,其中的信息可能已经有所发展或是发生改变。
    wordpress 的侧边栏很强大,各种最近或者随机的文章,貌似对 seo 比较友好

    如何在 django 的 sidebar 中实现随机文章?
    8 条回复    2015-10-08 10:55:54 +08:00
    awanabe
        1
    awanabe  
       2015-10-07 11:50:45 +08:00
    自己写啊....
    伸手党简直了...
    不会写代码就用 wordpress 好了...
    XianZaiZhuCe
        2
    XianZaiZhuCe  
       2015-10-07 12:21:17 +08:00 via Android
    random()
    XianZaiZhuCe
        3
    XianZaiZhuCe  
       2015-10-07 12:22:03 +08:00 via Android
    不好意思,这是 php 的… python 应该也有对应的函数吧
    xiaket
        4
    xiaket  
       2015-10-07 16:25:05 +08:00
    拿 Django 对比 wordpress 说明你还不懂 Django.
    twor2
        5
    twor2  
       2015-10-07 18:26:03 +08:00
    最直观的办法,用随机排序查询, Entry.objects.order_by('?'),再取 TOP10 之类,但性能不好。

    https://docs.djangoproject.com/en/1.8/ref/models/querysets/

    http://quartergeek.com/select-one-random-line-from-mysql-database/
    pc10201
        6
    pc10201  
    OP
       2015-10-07 22:20:26 +08:00
    @twor2 我也是这个思路,但是对应的 templatetags 我没有写出来
    twor2
        7
    twor2  
       2015-10-08 00:46:45 +08:00




    随便测试了 10000 条数据的查询,如果是个人博客,应该没啥问题。
    pc10201
        8
    pc10201  
    OP
       2015-10-08 10:55:54 +08:00
    @twor2 我将 sidebar.html 单独拆分出来了,因为许多页面都要用到,所以上面的这种不适用~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   780 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 22:09 · PVG 06:09 · LAX 15:09 · JFK 18:09
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.