如何在 Django 中创建独特的 slug
我正在尝试在 Django 中创建一个独特的 slug,以便我可以通过如下网址访问帖子: 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 = "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,那就太好了!
I am trying to create a unique slug in Django so that I can access a post via a url like this:
http://www.example.com/buy-a-new-bike_Boston-MA-02111_2
The relevant models:
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
A sample ZipCode record:
- zipcode = "02111"
- city = "Boston"
- statecode = "MA"
A sample Need record:
- title = "buy a new bike"
- us_zip = "02111"
- slug = "buy-a-new-bike_Boston-MA-02111_2" (desired)
Any tips as to how to create this unique slug? Its composition is:
- Need.title + "_" + Need.get_city() + "_" + an optional incrementing integer to make it unique. All spaces should be replaced with "-".
NOTE: My desired slug above assumes that the slug "buy-a-new-bike_Boston-MA-02111" already exists, which is what it has the "_2" appended to it to make it unique.
I've tried django-extensions, but it seems that it can only take a field or tuple of fields to construct the unique slug. I need to pass in the get_city() function as well as the "_" connector between the title and city. Anyone solved this and willing to share?
Thank you!
UPDATE
I'm already using django-extensions for its UUIDField, so it would be nice if it could also be usable for its AutoSlugField!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
我使用这个 snippet 来生成独特的 slug,我典型的保存方法如下所示,
slug 将是 Django SlugField 与Blank=True 但在保存方法中强制执行 slug。
Need 模型的典型保存方法可能如下所示
,这将生成类似 buy-a-new-bike_Boston-MA-02111 、 buy-a-new-bike_Boston-MA-02111-1 等的 slug。输出可能略有不同,但您始终可以查看代码片段并根据您的需求进行自定义。
I use this snippet for generating unique slug and my typical save method look like below
slug will be Django SlugField with blank=True but enforce slug in save method.
typical save method for Need model might look below
and this will generate slug like buy-a-new-bike_Boston-MA-02111 , buy-a-new-bike_Boston-MA-02111-1 and so on. Output might be little different but you can always go through snippet and customize to your needs.
我的小代码:
My little code:
这是我使用的几个函数。您将模型实例和所需的标题传递到
unique_slugify
中,如果 slug 不存在,它将添加该 slug,否则它将继续尝试附加 4 位随机字符串,直到找到唯一的字符串。我通常通过重写模型
save
方法来使用它。Here are a couple functions that I use. You pass in the model instance and the desired title into
unique_slugify
which will add the slug if it doesn't exist, otherwise it will continue trying to append a 4 digit random string until it finds a unique one.I usually use it by overriding the model
save
method.如果您正在考虑使用应用程序来为您做到这一点,这里有一个。
https://github.com/un33k/django-uuslug
If you are thinking of using an app to do it for you, here is one.
https://github.com/un33k/django-uuslug
这是我用来生成独特的 slug 的简单而小的代码,
你只需要一个字段来创建你独特的slug字段
我希望你喜欢这个。
This is the simple and small code i am using for generating unique slug,
you only need one field to create your unique slug field
I hope you like this.
这是一个从标题生成 slug 的简单实现,它不依赖于其他片段:
This is a simple implementation that generate the slug from the title, it doesn't depend on other snippets:
Django 提供了一个 SlugField 模型字段来让您更轻松地完成此操作。这是“博客”应用程序中的一个示例,
请注意,我们为 slug 字段设置了 unique=True - 在这个项目中,我们将通过其 slug 查找帖子,因此我们需要确保它们是唯一的。我们的应用程序的views.py 可能如下所示:
Django provides a SlugField model field to make this easier for you. Here's an example of it in a "blog" app's
Note that we've set unique=True for our slug field — in this project we will be looking up posts by their slug, so we need to ensure they are unique. Here's what our application's views.py might look like to do this:
from django.utils.text import slugify 有很大帮助并且概念非常清晰。
这里有一个关于如何使用 from django.utils.text import slugify
utils.py
models.py
自动生成 slug 的示例Django 文档解释了 Django.utils.text import slugify 自动生成 slug 。您可以在此处
之后 阅读更多详细信息实现代码,在创建产品时,您可以将 slug 字段留空,这将通过自动生成的产品 slug 进一步获取,在本例中该字段是唯一的。
from django.utils.text import slugify Helps a lot and has quite clear Concepts.
Here one example on How to auto-generate slug by using from django.utils.text import slugify
utils.py
models.py
Django documentation explains Django.utils.text import slugify to generate slug automatically. You can read more detail here
After implementing the code, while creating product, you may leave the slug field blank, which will be further aquired with auto generated slug for the product which will be unique in this case.
您好,您可以尝试一下这个功能吗
Hi can you tried this function
对我来说最好的解决方案:
这段代码可以生成像这样的slug:
Best solution for me:
This code can generate slug like this:
试试这个,为我解决了,提前欢迎:
Try this, worked out for me,welcome in advance: