[英]Replace all spaces to dash in all my "url" object from my article model
I would like to replace all the spaces in my defined url objects from my Article model to "-".我想将文章 model 中定义的 url 对象中的所有空格替换为“-”。 However, my code below doesn't seem to work.
但是,我下面的代码似乎不起作用。
def index(request):
change_urls = Article.objects.all()
for i in change_urls:
i.url.replace(" ", "-")
i.save()
.replace(..)
creates a new string, it does not modify the string. .replace(..)
创建一个新字符串,它不会修改字符串。 You can thus work with:因此,您可以使用:
def index(request):
change_urls = list(Article.objects.all())
for i in change_urls:
i.url = i.url.replace(' ', '-')
Article.objects.bulk_update(change_urls, fields=('url',))
# …
But if you want to "slugify", please use the slugify(…)
function [Django-doc] .但是如果你想“slugify”,请使用
slugify(…)
函数[Django-doc] 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.