Ihor
Ihor

Reputation: 117

How to create a slug from non-English values?

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?

Upvotes: 2

Views: 1792

Answers (2)

Thierno Amadou Sow
Thierno Amadou Sow

Reputation: 2573

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

Upvotes: 3

lucutzu33
lucutzu33

Reputation: 3700

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: %20, %C3 you can use

from urllib.parse import quote
author_slug = slugify( quote(first_name + " " + last_name) )

Upvotes: 0

Related Questions