slugify 函数的奇怪行为
它在管理站点上完美运行。但下面的代码在某些语言中无法正常工作(缺少某些字符,例如土耳其语“ı”)。
class Foo(models.Model):
name = models.CharField(max_length=50, unique=True, db_index=True)
slug = models.SlugField(max_length=100, unique=True, db_index=True)
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.name)
super(Foo, self).save(*args, **kwargs)
例如,假设名称是“ışçğö”,然后 slug 变成“scgo”,而它应该是“iscgo”。
It works perfectly from the admin site. But the code below doesn't work properly(some characters are missing, like Turkish "ı") in some languages.
class Foo(models.Model):
name = models.CharField(max_length=50, unique=True, db_index=True)
slug = models.SlugField(max_length=100, unique=True, db_index=True)
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.name)
super(Foo, self).save(*args, **kwargs)
For example, let's assume that the name is "ışçğö" and then slug becomes "scgo" when it should be "iscgo" instead.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是
SlugField
行为根据定义。 slug 应该是 URL 的一部分。尽管 URL 可能支持非拉丁字符,但这些字符在 slugs 内不受支持。您获得的结果与 Django 行为不一致:
您到底从哪里获得这些结果?
This is
SlugField
behavior by definition. A slug is supposed to be part of a URL. Even though URLs might support non-latin characters, these are not supported inside slugs.The results you are getting aren't consistent with Django behavior:
Where exactly are you getting these results?
尝试 slughifi 函数更好的 slug 功能(感谢 Markus 向我展示了这一点)。
Try the slughifi function for better slug functionality (thanks to Markus for showing me this).