X
X
xozzslip2015-07-04 13:50:50
Django
xozzslip, 2015-07-04 13:50:50

How to transliterate urls in django?

Is it possible to make the database field be in Russian, and the link to it be transliterated into English.

deck_name = 'физика'
url = 'app/fizika'

I tried to do it through templatetags, but soon I realized that just by changing the url, the transmitted value also changes. That is, instead of 'physics', 'fizika' will be passed, and there is no field with that name in the database. Which suggests that this can only be done by adding a new field. But how do you make a new field that will default to the value of another field affected by the function?
Like this. C upper case for simplicity.
class Deck(models.Model):
        name = models.CharField(max_length = 30)
        name_eng  = models.CharField(max_length=30, default=name.upper())

UPD: I want to warn newbies who will use slug: keep in mind that when parsing urls, the regular expression should be [\w-], and not just \w, because in slug spaces are replaced by'-'

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Kitaev, 2015-07-04
@xozzslip

This is called slug (or CNC, if in Russian). Unfortunately, the jungian slugify function does not understand Cyrillic, so for one of my projects I wrote a wrapper for Cyrillic. You can see it here: https://gist.github.com/deliro/01851103f7a0206fe66d

class Deck(models.Model):
    slug = models.SlugField()
    name = models.CharField(max_length=30)

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

A
Alexander Pinkevich, 2015-07-04
@pinkevich

pip install pytils
from pytils.translit import slugify

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question