2

Django's slugify function works fine for creating valid slugs, but automatically converts all letters to lowercase. Many sites such as Wikipedia preserve case when creating slugs, though URL resolution is case insensitive. We want to do this too. We also want the slug to be editable for a specified time window before being locked.

I know that I can create a custom slugify function to handle this. But given how common this is, I'm wondering if there already exists a library or code snippet that does this, or at least makes this task easier. What is the simplest approach to create slugs with the following characteristics?

  1. all characters are valid slug characters
  2. case is preserved
  3. slug may be changed within a specified time window after creation, then locked thereafter (the idea is that the title may be corrected or changed during the initial 3 hour creation period, so the slug should change to reflect the new title during this gestation period)
Community
  • 1
  • 1
Joe Golton
  • 612
  • 6
  • 12
  • The term slug is derived from [web publishing](http://en.wikipedia.org/wiki/Slug_(web_publishing)#Slug). As to _why_ , I guess it because it just serves the purpose for the most common usecases. I dont think the reason would be documented anywhere. – karthikr Sep 27 '13 at 19:06
  • Also not all characters [should be allowed](http://stackoverflow.com/questions/1856785/characters-allowed-in-a-url) and as to your #3 - an intervalled slug change isn't something a custom slugify function should take care of. – Henrik Andersson Sep 28 '13 at 08:49
  • @limelights Agreed. Django slugify does a fine job of disallowing the invalid characters. But it has no option for preserving upper case letters. I also agree with you that #3 will best be handled outside the slugify function. I only mentioned it in case someone had heard of a library or app that had richer slug functionality, perhaps including all 3 of my points. I'm thinking at the moment that my best bet may be to copy Django's slugify code, name it something else, and alter it to include preserve case. I think I can handle #3 in models.py. – Joe Golton Sep 28 '13 at 19:23
  • For #3 something external should take care of the timing. However, any automatic sluggify needs to be turned off once the slug is deemed to be permanent. – Bryce Oct 02 '13 at 16:40

1 Answers1

0

Using Django 2.1 or above, I created my link like this and have a title case slug:

{% url 'view-model' item.effect.name|slugify|title item.effect.id %}

By calling the title method after slugify, it does what I need.

Jorge
  • 111
  • 1
  • 1
  • 6