无法检测来自 pre_save 信号的文本 "Notebook 9 Pro" 的语言。 Django 2.1,Python 3.7
Can't detect language for the text "Notebook 9 Pro" given from pre_save signal. Django 2.1, Python 3.7
我想在 pre_save signal
(填写模型名称时)的帮助下在管理员中自动创建 slug,但我遇到下一个错误:无法检测文本的语言 "Notebook 9 Pro"给出。
我知道,可以使用 prepopulated_fields
,但我需要使用 pre_save 信号的方法。
谢谢
models.py
class Product(models.Model):
title = models.CharField(max_length=120)
slug = models.SlugField(blank=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('product_detail', kwargs={'product_slug': self.slug})
def pre_save_product_slug(sender, instance, *args, **kwargs):
if not instance.slug:
slug = slugify(translit(str(instance.title), reversed=True))
instance.slug = slug
pre_save.connect(pre_save_product_slug, sender=Product)
回溯
/media/m0nte-cr1st0/43338d5a-aa90-411c-a6d7-96964c46e415/m0nte-cr1st0/programming/django_projects/djangoshop/ecomapp/models.py in pre_save_product_slug
slug = slugify(translit(str(instance.title), reversed=True)) ...
▶ Local vars
/media/m0nte-cr1st0/43338d5a-aa90-411c-a6d7-96964c46e415/m0nte-cr1st0/programming/django_projects/myvenv/lib/python3.5/site-packages/transliterate/utils.py in translit
language_code = detect_language(value, fail_silently=False) ...
▶ Local vars
/media/m0nte-cr1st0/43338d5a-aa90-411c-a6d7-96964c46e415/m0nte-cr1st0/programming/django_projects/myvenv/lib/python3.5/site-packages/transliterate/utils.py in detect_language
_("""Can't detect language for the text "%s" given.""") % text ...
▶ Local vars
如果我输入英文字符,我会收到错误消息。如果我输入俄语字符,slug 不会将它们翻译成英语。
我需要一个鼻涕虫来组成 url。
您应该将语言作为第二个参数添加到 translit
函数:
from transliterate import translit
def pre_save_product_slug(sender, instance, *args, **kwargs):
if not instance.slug:
slug = slugify(translit(str(instance.title), 'ru', reversed=True))
instance.slug = slug
我想在 pre_save signal
(填写模型名称时)的帮助下在管理员中自动创建 slug,但我遇到下一个错误:无法检测文本的语言 "Notebook 9 Pro"给出。
我知道,可以使用 prepopulated_fields
,但我需要使用 pre_save 信号的方法。
谢谢
models.py
class Product(models.Model):
title = models.CharField(max_length=120)
slug = models.SlugField(blank=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('product_detail', kwargs={'product_slug': self.slug})
def pre_save_product_slug(sender, instance, *args, **kwargs):
if not instance.slug:
slug = slugify(translit(str(instance.title), reversed=True))
instance.slug = slug
pre_save.connect(pre_save_product_slug, sender=Product)
回溯
/media/m0nte-cr1st0/43338d5a-aa90-411c-a6d7-96964c46e415/m0nte-cr1st0/programming/django_projects/djangoshop/ecomapp/models.py in pre_save_product_slug
slug = slugify(translit(str(instance.title), reversed=True)) ...
▶ Local vars
/media/m0nte-cr1st0/43338d5a-aa90-411c-a6d7-96964c46e415/m0nte-cr1st0/programming/django_projects/myvenv/lib/python3.5/site-packages/transliterate/utils.py in translit
language_code = detect_language(value, fail_silently=False) ...
▶ Local vars
/media/m0nte-cr1st0/43338d5a-aa90-411c-a6d7-96964c46e415/m0nte-cr1st0/programming/django_projects/myvenv/lib/python3.5/site-packages/transliterate/utils.py in detect_language
_("""Can't detect language for the text "%s" given.""") % text ...
▶ Local vars
如果我输入英文字符,我会收到错误消息。如果我输入俄语字符,slug 不会将它们翻译成英语。
我需要一个鼻涕虫来组成 url。
您应该将语言作为第二个参数添加到 translit
函数:
from transliterate import translit
def pre_save_product_slug(sender, instance, *args, **kwargs):
if not instance.slug:
slug = slugify(translit(str(instance.title), 'ru', reversed=True))
instance.slug = slug