Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在slug中编写波斯语,并在django的地址栏中使用它_Python_Django_Persian - Fatal编程技术网

Python 在slug中编写波斯语,并在django的地址栏中使用它

Python 在slug中编写波斯语,并在django的地址栏中使用它,python,django,persian,Python,Django,Persian,我使用django,在我的模型中,我想在slugfield中编写波斯语(通过使用utf-8或其他东西),并在页面地址中使用slug 我为模型编写了这个类: class Category(models.Model): name = models.CharField(max_length=20, unique=True) slug = models.SlugField(max_length=20, unique=True) description = models.CharF

我使用django,在我的模型中,我想在slugfield中编写波斯语(通过使用utf-8或其他东西),并在页面地址中使用slug 我为模型编写了这个类:

class Category(models.Model):
    name = models.CharField(max_length=20, unique=True)
    slug = models.SlugField(max_length=20, unique=True)
    description = models.CharField(max_length=500)
    is_active = models.BooleanField(default=False)
    meta_description = models.TextField(max_length=160, null=True, blank=True)
    meta_keywords = models.TextField(max_length=255, null=True, blank=True)
    user = models.ForeignKey(settings.AUTH_USER_MODEL)

    def save(self, *args, **kwargs):
        self.slug = slugify(self.name)
        super(Category, self).save(*args, **kwargs)

    def __str__(self):
        return self.name

    def category_posts(self):
        return Post.objects.filter(category=self).count()
但在保存后的slug列中并没有任何内容,我不知道在url中写什么来显示它。你能告诉我该怎么办吗

我使用django 1.9和python 3.6。

slagify函数的

如果“allow_unicode”为False,则转换为ASCII。将空格转换为连字符。 删除非字母数字、下划线或连字符的字符。 转换成小写。同时去掉前导和尾随空格

因此,您需要将
allow_unicode
标志设置为
True
,以保留波斯语文本

>>> text = 'سلام عزیزم! عزیزم سلام!'
>>> slugify(text)
''
>>> slugify(text, allow_unicode=True)
'سلام-عزیزم-عزیزم-سلام'
>>> 
这更好

‍‍‍slug = models.SlugField(max_length=20, unique=True, allow_unicode=True)

下面是一个例子,你可以在这个案例中使用:

如果未安装,请首先使用
pip
安装django_扩展

from django_extensions.db.fields import AutoSlugField
from django.utils.text import slugify
在model.py中,在类之前添加此函数:

def my_slagify_函数(内容): 返回slugify(内容,allow_unicode=True)

在类中添加此字段:

slug=AutoSlugField(从=['name']填充),unique=True,allow\u unicode=True, slagify\u函数=我的slagify\u函数)

中的url必须使用以下格式:

re_path('person_list/(?P<slug>[-\w]+)/', views.detail, name='detail')
re_路径('person_list/(?P[-\w]+)/',views.detail,name='detail'))

你能在回答中解释为什么“这样更好!!”吗?