Django slugfield '数字、字母、下划线和连字符'似乎没有强制执行
我有一个如下所示的模型:
class Page(ContentBase):
url_slug = models.SlugField()
根据 SlugField 文档,slugfield 是“数字、字母、下划线和连字符”。然而,我似乎能够设置坏的 slugs,其中的字符超出了该规范:
page = Page.objects.get(id=872)
page.url_slug = '&*()&*(*(Y*'
page.save()
In [26]: page.url_slug
Out[26]: '&*()&*(*(Y*'
这是为什么? SlugFields 是否应该根据文档实际验证其输入,还是我需要自己执行此操作?当我似乎能够如此轻松地避免它时,为什么文档会说明限制?
I have a model like the following:
class Page(ContentBase):
url_slug = models.SlugField()
Per the SlugField docs, slugfields are 'numbers, letters, underscores and hyphens'. However I seem to be able to set bad slugs which have characters outside that spec:
page = Page.objects.get(id=872)
page.url_slug = '&*()&*(*(Y*'
page.save()
In [26]: page.url_slug
Out[26]: '&*()&*(*(Y*'
Why is this? Should the SlugFields actually be validating its input per the docs, or do I need to do this myself? Why does the documentation state the limit when I seem to be able to avoid it so easily?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SlugField 的验证通过其匹配的 表单< /strong>.SlugField:
如果没有表单手动修改,参考django.core.validators.validate_slug:
SlugField's validation works through it's matching forms.SlugField:
If you modify it manually without a form, refer to django.core.validators.validate_slug: