Created
November 10, 2020 12:25
-
-
Save muradddd/26fb29f78934433347798ae3bb6ff66d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from unidecode import unidecode | |
def slugify(title): | |
symbol_mapping = ( | |
('ə', 'e'), | |
('ı', 'i'), | |
('ö', 'o'), | |
('ğ', 'g'), | |
('ü', 'u'), | |
('ş', 's'), | |
('ç', 'c'), | |
(' ', '-'), | |
) | |
title_url = title.strip().lower() | |
for before, after in symbol_mapping: | |
title_url = title_url.replace(before, after) | |
for symbol in title_url: | |
if symbol in string.punctuation: | |
title_url = title_url.replace(symbol,'-') | |
title_url = title_url.strip('-') | |
title_url_list = list(title_url) | |
for i in range(len(title_url_list)): | |
if title_url_list[i] == '-' and title_url_list[i+1] == '-': | |
title_url_list[i] = '' | |
title_url = ''.join(title_url_list) | |
return unidecode(title_url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import slugify | |
class Review(models.Model): | |
slug = models.SlugField(max_length=255, null=True, blank=True) | |
class Meta: | |
verbose_name = "Review" | |
verbose_name_plural = "Reviews" | |
def __str__(self): | |
return self.title | |
def save(self, *args, **kwargs): | |
super(Review, self).save(*args, **kwargs) | |
self.slug = f'{slugify(self.title)}-{slugify(str(self.created_at.timestamp()))}' | |
super(Review, self).save(*args, **kwargs) | |
def get_absolute_url(self): | |
return reverse_lazy("reviews:review-detail", kwargs={"slug": self.slug}) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
path('detail/<slug:slug>/', ReviewDetailView.as_view(), name="review-detail"), |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment