如何在Django中创建一个独特的slug

时间:2010-09-28 19:25:13

标签: django django-models slug

我正在尝试在Django中创建一个独特的slug,以便我可以通过这样的URL访问帖子: http://www.example.com/buy-a-new-bike_Boston-MA-02111_2

相关模型:

class ZipCode(models.Model):
    zipcode = models.CharField(max_length=5)
    city = models.CharField(max_length=64)
    statecode = models.CharField(max_length=32)

class Need(models.Model):
    title = models.CharField(max_length=50)
    us_zip = models.CharField(max_length=5)
    slug = ?????

    def get_city():
        zip = ZipCode.objects.get(zipcode=self.us_zip)
        city = "%s, %s %s" % (zip.city, zip.statecode, zip.zipcode)
        return city

ZipCode记录示例:

  • zipcode =“02111”
  • city =“Boston”
  • statecode =“MA”

样本需求记录:

  • title =“买一辆新自行车”
  • us_zip =“02111”
  • slug =“buy-a-new-bike_Boston-MA-02111_2”(所需)

有关如何创建这个独特的slug的任何提示?它的组成是:

  • Need.title +“_”+ Need.get_city()+“_”+一个可选的递增整数,使其唯一。所有空格都应替换为“ - ”。

注意:上面我想要的slug假定slug“buy-a-new-bike_Boston-MA-02111”已经存在,这就是它附加了“_2”以使其独一无二。

我尝试过django-extensions,但它似乎只能用一个字段或字段来构造独特的slug。我需要传入get_city()函数以及标题和城市之间的“_”连接符。任何人都解决了这个并愿意分享?

谢谢!

更新

我已经在为其UUIDField使用django-extensions,所以如果它也可用于其AutoSlugField会很好!

12 个答案:

答案 0 :(得分:36)

我使用此snippet生成唯一的slug,我的典型保存方法如下所示

slug将是Django SlugField,其中为blank = True,但在save方法中强制执行slug。

Need模型的典型保存方法可能如下所示

def save(self, **kwargs):
    slug_str = "%s %s" % (self.title, self.us_zip) 
    unique_slugify(self, slug_str) 
    super(Need, self).save(**kwargs)

这将产生像买一辆新自行车_Boston-MA-02111,买一辆新自行车_Boston-MA-02111-1等等。输出可能略有不同,但您可以随时查看代码段并根据需要进行自定义。

答案 1 :(得分:6)

我的小代码:

def save(self, *args, **kwargs):
    strtime = "".join(str(time()).split("."))
    string = "%s-%s" % (strtime[7:], self.title)
    self.slug = slugify(string)
    super(Need, self).save()

答案 2 :(得分:5)

如果您正在考虑使用应用程序为您执行此操作,则此处为一个。

https://github.com/un33k/django-uuslug

UUSlug = (``U``nique + ``U``code Slug)


Unicode Test Example
=====================
from uuslug import uuslug as slugify

s = "This is a test ---"
r = slugify(s)
self.assertEquals(r, "this-is-a-test")

s = 'C\'est déjà l\'été.'
r = slugify(s)
self.assertEquals(r, "c-est-deja-l-ete")

s = 'Nín hǎo. Wǒ shì zhōng guó rén'
r = slugify(s)
self.assertEquals(r, "nin-hao-wo-shi-zhong-guo-ren")

s = '影師嗎'
r = slugify(s)
self.assertEquals(r, "ying-shi-ma")


Uniqueness Test Example
=======================
Override your objects save method with something like this (models.py)

from django.db import models
from uuslug import uuslug as slugify

class CoolSlug(models.Model):
    name = models.CharField(max_length=100)
    slug = models.CharField(max_length=200)

    def __unicode__(self):
        return self.name

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

Test:
=====

name = "john"
c = CoolSlug.objects.create(name=name)
c.save()
self.assertEquals(c.slug, name) # slug = "john"

c1 = CoolSlug.objects.create(name=name)
c1.save()
self.assertEquals(c1.slug, name+"-1") # slug = "john-1"

答案 3 :(得分:2)

这是一个简单的实现,可以从标题中生成slug,它不依赖于其他代码片段:

from django.template.defaultfilters import slugify

class Article(models.Model):
    ...
    def save(self, **kwargs):
        if not self.slug:
            slug = slugify(self.title)
            while True:
                try:
                    article = Article.objects.get(slug=slug)
                    if article == self:
                        self.slug = slug
                        break
                    else:
                        slug = slug + '-'
                except:
                    self.slug = slug
                    break

        super(Article, self).save()

答案 4 :(得分:2)

这是我用来生成唯一子弹的简单小代码, 您只需要一个字段即可创建自己的唯一子弹字段

from random import randint

def save(self, *args, **kwargs):
    if Post.objects.filter(title=self.title).exists():
        extra = str(randint(1, 10000))
        self.slug = slugify(self.title) + "-" + extra
    else:
        self.slug = slugify(self.title)
    super(Post, self).save(*args, **kwargs)

我希望你喜欢这个。

答案 5 :(得分:1)

Django提供了一个SlugField模型字段,让您更轻松。以下是#34;博客中的一个示例"应用程序

class Post(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField(blank=True)

    slug = models.SlugField(unique=True)

    @models.permalink
    def get_absolute_url(self):
        return 'blog:post', (self.slug,)

请注意,我们为slug字段设置了unique = True - 在这个项目中,我们将通过slug查找帖子,因此我们需要确保它们是唯一的。以下是我们的应用程序的views.py可能会这样做:

from .models import Post

def post(request, slug):
    post = get_object_or_404(Post, slug=slug)

    return render(request, 'blog/post.html', {
        'post': post,
    })

答案 6 :(得分:0)

您好,您可以尝试使用此功能

class Training(models.Model):
    title = models.CharField(max_length=250)
    text = models.TextField()
    created_date = models.DateTimeField(
    auto_now_add=True, editable=False, )
    slug = models.SlugField(unique=True, editable=False, max_length=250)

    def __unicode__(self):
       return self.title

    def save(self, *args, **kwargs):
       self.slug =get_unique_slug(self.id,self.title,Training.objects)
       return super(Training, self).save(*args, **kwargs)

def get_unique_slug(id,title,obj):
    slug = slugify(title.replace('ı', 'i'))
    unique_slug = slug
    counter = 1
    while obj.filter(slug=unique_slug).exists():
       if(obj.filter(slug=unique_slug).values('id')[0]['id']==id):
           break
       unique_slug = '{}-{}'.format(slug, counter)
       counter += 1
    return unique_slug

答案 7 :(得分:0)

尝试一下,为我解决,请提前:

imagename:1.2.3

答案 8 :(得分:0)

django.utils.text中的

导入slugify很有帮助,并且概念非常清晰。 这里是一个有关如何使用django.utils.text import slugify自动生成弹头的示例

utils.py

#include <iostream>
#include <windows.h>
#include <fstream>
using namespace std;

void ReadData (int[] , int);
int main()
{
    int product_code[9999];
    int code , num;
    ofstream outdata;
    ReadData (product_code , 9999);

    outdata.open("productlist.txt");
    cout << "How many product code?";
    cin >> num;
    for(int i=0 ; i<num ; i++)
    {
        cout << "Product Code : ";
        cin >> code;
    }
    outdata.close();

    for(int i=0 ; i<9999 ; i++)
    {
        cout << product_code[i] << endl;
    } 
    system("pause");
    return 0;       
}  

void ReadData(int p_code[] , int j)
{
    ifstream indata;
    indata.open("productlist.txt");
    while (indata >> p_code[j])
    {
        j++;
    }
    indata.close();
}

models.py

from django.utils.text import slugify
import random
import string

# Random string generator
def random_string_generator(size=10, chars=string.ascii_lowercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))

# Unique Slug Generator 
def unique_slug_generator(instance, new_slug=None):
    """
    It assumes your instance has a model with a slug field and a title character (char) field.
    """
    if new_slug is not None:
        slug = new_slug  
    else:
        slug = slugify(instance.title)  

    Klass = instance.__class__

    qs_exists = Klass.objects.filter(slug=slug).exists()

    if qs_exists:
        new_slug = "{slug}-{randstr}".format(slug=slug, randstr=random_string_generator(size=4))
        return unique_slug_generator(instance, new_slug=new_slug)
    return slug

Django文档解释了Django.utils.text import slugify可以自动生成slug。您可以阅读更多详细信息here

实现代码后,在创建产品时,可以将slug字段留为空白,这将由自动生成的产品的Slug进一步要求,在这种情况下,它将是唯一的。

答案 9 :(得分:0)

这是我使用的几个功能。您将模型实例和所需的标题传递到unique_slugify中,如果不存在该段,则会添加该段,否则它将继续尝试附加4位随机字符串,直到找到唯一的字符串为止。

import string
from django.utils.crypto import get_random_string

def unique_slugify(instance, slug):
    model = instance.__class__
    unique_slug = slug
    while model.objects.filter(slug=unique_slug).exists():
        unique_slug = slug + get_random_string(length=4)
    return unique_slug

我通常通过覆盖模型save方法来使用它。

class YourModel(models.Model):
    slug = models.SlugField()
    title = models.CharField()

    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = unique_slugify(self, slugify(self.title))
        super().save(*args, **kwargs)

答案 10 :(得分:0)

只需尝试将唯一值组合在一起,例如: 我要一起消耗时间,用户名和机票主题,并将其另存为一条,如下所示:

from django.utils.text import slugify 
from datetime import datetime


time_now = datetime.now().strftime('%Y-%m-%d_%H:%M:%S')
class ticket(models.Model):
    TKT_USER = models.ForeignKey(User, related_name='TKT_USER', on_delete=models.CASCADE)
    TKT_SUB = models.CharField(max_length=20, db_index=True, verbose_name="ticket subject")
    TKT_BOD = models.TextField(verbose_name="ticket body")
    TKT_SLug = models.SlugField(blank=True, null=True, db_index=True, unique=True)



    def save(self , *args , **kwargs):
         if not self.TKT_SLug:
            data_to_slug = f"{time_now}_{self.TKT_USER}_{self.TKT_SUB}"
            self.TKT_SLug = slugify(data_to_slug)
            super(ticket , self).save(*args, **kwargs)

答案 11 :(得分:-1)

class Need(models.Model):
    title = models.CharField(max_length=50)
    us_zip = models.CharField(max_length=5)
    slug = models.SlugField(unique=True)

    def save(self, **kwargs):
        slug_str = "%s %s" % (self.title, self.us_zip) 
        super(Need, self).save()