Login

Tag "slug"

20 snippets

Snippet List

Unique Slugify

Automatically create a unique slug for a model. Note that you *don't* need to do `obj.slug = ...` since this method updates the instance's slug field directly. All you usually need is: `unique_slugify(obj, obj.title)` A frequent usage pattern is to override the `save` method of a model and call `unique_slugify` before the `super(...).save()` call.

  • slug
Read More

jQuery slugify plugin

This plugin lets you make a field(ideally for a slug) populate itself based on the value of another field. You use it like this: jQuery('#id_title').slugify('#id_slug');

  • slug
  • jquery
  • slugify
Read More

Using the built-in slugify filter outside a template

This is just a very short (and mostly useless on it's own) example of how the built in slugify filter can be used in a Python script to generate slugs. It was pulled from a script I've written to pull in items from Upcoming.org's API. I post it because "sunturi" posted [Snippet #29](http://www.djangosnippets.org/snippets/29/), which duplicates the functionality of the built-in slugify filter. In the comments of that snippet, santuri (and others) seem to believe that template filters can only be used within templates. This is incorrect, and I think it's important people understand they can be used elsewhere. sunturi's snippet does remove prepositions from values before slugifying them, so if you need that, his code will work work. But if all you need is slugification, the built-in slugify filter will work fine -- in a Python script, as well as in a template.

  • template
  • filter
  • django
  • slug
  • built-in
Read More

Automate unique slugs

If you want unique values for a slug field, but don't want to bother the user with error messages, this function can be put into a model's save function to automate unique slugs. It works by appending an integer counter to duplicate slugs. The item's slug field is first prepopulated by slugify-ing the source field. If that value already exists, a counter is appended to the slug, and the counter incremented upward until the value is unique. For instance, if you save an object titled Daily Roundup, and the slug daily-roundup is already taken, this function will try daily-roundup-2, daily-roundup-3, daily-roundup-4, etc, until a unique value is found. Call from within a model's custom save() method like so: `unique_slug(item, slug_source='field1', slug_field='field2')` where the value of field slug_source will be used to prepopulate the value of slug_field. Comments appreciated!

  • slug
  • save
Read More

Automatically slugify slug fields in your models

*I suppose I'm kind of stubborn, but I prefer to use underscores to replace spaces and other characters. Of course, that shouldn't hold you back from using the build-in slugify filter :)* ** Forcing the slug to use ASCII equivalents: ** Transforming titles like "Äës" to slugs like "aes" was kind of a trial and error job. It now works for me. I hope `_string_to_slug(s):` proves a rather stable solution. Yet the worst-case scenario is that such characters are lost, I guess that is acceptable. Other ways of dealing with this problem can be found at [Latin1 to ASCII at Activestate](http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/251871) or in the comments below. **How to use:** The slug fields in your model must have prepopulate_from set, the fields specified in it are used to build the slug. To prevent duplicates, a number is added to the slug if the slug already exists for the current field in another, previous, object. I guess there should be a cleaner way to distinguish between creating a new db entry or updating an existing one, sadly, the db back-end is kind of a black-box to me. At least this works ;) I choose not to alter the slug on an update to keep urls more bookmarkable. You could even extend this further by only updating the slug field if it hasn't been assigned a value.

  • slug
  • dispatcher
  • events
Read More

unique_slugify

sorry, this is a duplicate post, original is here: http://www.djangosnippets.org/snippets/690/ no way to delete snippets

  • slug
  • slugify
Read More

Automate unique slug (again)

This takes the "easier to ask forgiveness than permission" approach to making sure your model's slug is unique. If the model's slug is empty, make a slug from the model's 'name' field. We assume that it is a conflicting slug that is throwing the IntegrityError, in which case if the slug does not end with '-' followed by a number then append '-2'. If the slug does end with '-' followed by a number then capture that final number, increment it by one, save the new slug value and try savin g again.

  • slug
  • save
Read More

Auto slug field

New field type which allows prepopulate_from to work not only from javascript but in python too. If the slugfield has unique=True creates a unique slug too.

  • slug
  • field
  • auto
  • prepopulate_from
Read More

AutoSlugField

This is a simple solution aimed at saving some time when you simply want to get a slug from a model field just before saving.

  • fields
  • slug
  • field
Read More

Hierarchical page slugs

This allows for urls in the form of `/grandparent-slug/parent-slug/self-slug/` where the number of parent slugs could be 0 to many. You'll need to make sure that it is your last urlpattern because it is basically a catch-all that would supersede any other urlpatterns. Assumes your page model has these two fields: * `slug = models.SlugField(prepopulate_from=("title",), unique=True)` * `parent = models.ForeignKey("self", blank=True, null=True)`

  • slug
  • url
Read More

Slugify alternative

I prefer to use this slugification function rather than the one included with Django. It uses underscores instead of dashes for spaces, and allows dashes and periods to occur normally in the string. I decided on this when considering reasonable slugified titles such as... object-relational_mapper_2.5 ten_reasons_web-2.0_rocks django-trunk_0.99_updated

  • filter
  • slugs
  • slug
  • slugify
Read More

a better Django Slug Unique Generator

let say the user chooses the name "Elsa Frozen" now his slug would be "Elsa-Frozen-5" it means 4 other people have used the same header now he can go to url: "your website.com/Elsa-Frozen-4" to see other people's Post

  • django
  • slug
  • url
Read More

slugify with transliteration

This slugify correctly transliterates special characters using the translitcodec package from PyPI. Make sure you've installed http://pypi.python.org/pypi/translitcodec/ before using this.

  • slug
  • slugify
  • special chars
  • trans
  • transliteration
  • umlauts
Read More