- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我开发了一个表单,用户可以在其中添加他/她的 first name
和 last name
。
对于 username
(unique
属性),我设计了以下方法:
FirstName
:harrY LastName
:PottEr --> username
:Harry-Potter
FirstName
:HARRY LastName
:POTTER --> username
:Harry-Potter-1
FirstName
:harrY LastName
:PottEr --> username
:Harry-Potter-2
等等..
这是我的函数定义:
def return_slug(firstname, lastname):
u_username = firstname.title()+'-'+lastname.title() //Step 1
u_username = '-'.join(u_username.split()) //Step 2
count = User.objects.filter(username=u_username).count() //Step 3
if count==0:
return (u_username)
else:
return (u_username+'-%s' % count)
在实现第 3 步
之前,我不知道该做什么。我应该在哪里放置 [:len(u_username)]
来比较字符串?
编辑:
如果 Harry-Potter
有多个实例,则应用此方法,解决最后添加整数的问题。我的问题是:我将如何检查附加到 Harry-Potter
的最后一个整数是什么?
最佳答案
试试这个:
from django.utils.text import slugify
def return_slug(firstname, lastname):
# get a slug of the firstname and last name.
# it will normalize the string and add dashes for spaces
# i.e. 'HaRrY POTTer' -> 'harry-potter'
u_username = slugify(unicode('%s %s' % (firstname, lastname)))
# split the username by the dashes, capitalize each part and re-combine
# 'harry-potter' -> 'Harry-Potter'
u_username = '-'.join([x.capitalize() for x in u_username.split('-')])
# count the number of users that start with the username
count = User.objects.filter(username__startswith=u_username).count()
if count == 0:
return u_username
else:
return '%s-%d' % (u_username, count)
关于python - 在 Django 中对字符串进行 Slugify,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16696886/
我正在使用angular-slugify从标题输入中制作一个片段。但有什么办法可以恢复原状吗?我是 angularjs 新手,似乎没有用于标题恢复的资源。 UPDATE 如何在 angularjs 或
如果我有一个名为 Object 1 的对象,并且我想要 get() 该对象,但我试图使用一个 slugified 名称来获取它 object-1,有什么办法可以做到这一点吗?像这样的东西: Model
我的模型在slugify中有问题 from django.db import models from django.contrib.auth.models import User from djang
我想要 SEO 友好的 URL,我在 urls.py 中的当前 url: (ur'^company/news/(?P.*)/(?P\d+)/$','CompanyHub.views.getNews')
一旦用户登录到我的网站,他就可以写一篇文章并更新它。 然后我在添加允许人们发表评论的功能方面取得了进展。我正处于可以从后端添加评论的阶段,它们会准确地显示在前端。 现在,当我尝试更新帖子时,我收到一条
当我想从 product 创建新对象时,出现此错误: slugify() got an unexpected keyword argument 'allow_unicode' 这是我的模型: clas
我有点卡在这个问题上: 众所周知,django 有一个 slugify 模板标签,它使 traume-von-bucher 脱离 Träume von Bücher。 但从句法上讲,Träume 应该
我有一个像这样的模型: class Thing(models.Model): title = models.CharField(_('Title'), max_length=255) 我想添加
我希望我的冒号变成破折号而不是空字符串。我想我可以将 slugify(self.name.replace(":", "-"))) 之类的东西放入我的 save() 方法中,但那不会完全干燥(我认为)。
我正在考虑通过 diegok 转换 JS slugify 函数他正在使用这个 JavaScript 构造: function turkish_map() { return {
我想对一个对象的字符串属性进行 slugify 以在 url 中使用它。我读到在 play 1.x 中有一个名为“slugify”的函数,但我在 play 2.x 中找不到它 在 play 2.x 中
我已经开始使用 symfony2,并且我发现了 symfony 1.4 中存在的一些功能,例如 slugify。我需要这个来改善毛发。 最佳答案 如果你有 php-intl : /** * Tran
我正在尝试将以下 slugify 方法从 PHP 转换为 C#: http://snipplr.com/view/22741/slugify-a-string-in-php/ 编辑:为了方便起见,将上
我正在尝试这个 from django.utils.text import slugify In [8]: mystr = "This is john" In [9]: slugify(mystr)
我开发了一个表单,用户可以在其中添加他/她的 first name 和 last name。 对于 username(unique 属性),我设计了以下方法: FirstName:harrY Last
我目前正在使用 Django 开发一个玩具项目。 我的应用程序的一部分允许用户发表评论。我想获取评论的标题并将其修改以创建一个 url。 因此,如果用户写了一篇名为“The best thing ev
单击帖子类别时出现错误 字段 'id' 需要一个数字,但得到了 'coding'。 每个帖子都添加在下面的类别下是我的代码: 模型: class Categories(models.Model):
ASP.NET Core 2.2 引入了一个使用 Parameter transformer 来 slugifying 路由 url 的选项如下: routes.MapRoute( name:
这个问题已经有答案了: How to make Django create slug from unicode characters? (3 个回答) 已关闭 8 年前。 我正在尝试对 UTF-8 文
我正在尝试使用 django 博客应用程序项目中的 slugified url 访问页面。 问题模板代码如下所示: {% for post in latest_post_list
我是一名优秀的程序员,十分优秀!