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
yanng
V2EX  ›  Python

求助 - 怎样让 django 的 slugfield 支持中文?

  •  
  •   yanng · 2011-12-20 21:13:58 +08:00 · 7698 次点击
    这是一个创建于 4516 天前的主题,其中的信息可能已经有所发展或是发生改变。
    6 条回复    1970-01-01 08:00:00 +08:00
    vicalloy
        1
    vicalloy  
       2011-12-20 21:23:56 +08:00
    用普通的charfield,没必要一定用slug
    ywjno
        2
    ywjno  
       2011-12-20 21:27:48 +08:00
    用encode的方式,
    不光是中文,各种不在asc码上的文字都能搞定了
    yanng
        3
    yanng  
    OP
       2011-12-20 22:19:38 +08:00
    @reus 谢谢,我去看看
    yanng
        4
    yanng  
    OP
       2011-12-20 22:35:22 +08:00
    @ywjno encode decode和不这样做直接显示汉字,在SEO或者其他方面有没有什么区别?
    lueo
        6
    lueo  
       2012-07-17 00:09:33 +08:00   ❤️ 1
    这个实现非常完整,也很稳定,针对django写的,运用的是undecode,运作如下:

    # -*- coding: utf-8 -*-
    from uuslug import uuslug as slugify
    s = u"这就是一串中文字,你看得懂吗?"
    print slugify(s)
    >>> zhe-jiu-shi-chuan-zhong-wen-zi-ni-kan-de-dong-ma

    https://github.com/un33k/django-uuslug
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2189 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 15:50 · PVG 23:50 · LAX 08:50 · JFK 11:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.