I have the model Author with fields firstname, lastname. I wanna add another field 'slug' that will contain a slug of a concatenation of the fields. However, these fields contain non-English chars and I need an English slug to create the link template "localhost/authors/str::slug" How can I implement it?
CodePudding user response:
To solve this problem you can use the unidecode
and slugify
.
you should install it by pip install unidecode
from unidecode import unidecode
from django.template import defaultfilters
slug = defaultfilters.slugify(unidecode(input_text))
example:
import unidecode
a = unidecode.unidecode('привет')
the answer will be ('privet') #this is just an example in russian.
and after that you can apply slugify
CodePudding user response:
You can first convert the name to latin characters using trans
.
pip install trans
from trans import trans
from django.utils.text import slugify
author_slug = slugify( trans(first_name " " last_name) )
Or if you don't mind characters like: ,