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
A pre_save signal that will automatically generate a slug for your model based on the "title" attribute, and will store the new slug in the "slug" attribute.
USAGE:
from django.db.models.signals import pre_save
from YOURPACKAGE import slug_generator
pre_save.connect(slug_generator, sender=YOURMODEL)
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.
Extra field for slugs that does the work.
If the slug value is given, the value is not recreated but correctness is ensured. If value is not given, the field regenerates the slug.
This code overrides the existing RegistrationForm in django-registration and adds a new validation step. In this step, the username (my example slug) is compared against all the existing URLs that the application currently resolves and, if it *does* successfully resolve, throws a validation exception. This indicates that the username chosen would be overriden (or, if you wrote your urls.py file badly, would override) an existing URL already recognized by your application.
A much longer explanation can be found at [Dynamic names as first-level URL path objects in Django](http://www.elfsternberg.com/2009/06/26/dynamic-names-as-first-level-url-path-objects-in-django/).
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');
This is the self-populating AutoSlugField I use. It's not the [first such snippet](http://www.djangosnippets.org/tags/slug/), but (IMO) it works a bit more cleanly. It numbers duplicate slugs (to avoid IntegrityErrors on a unique slug field) using an "ask-forgiveness-not-permission" model, which avoids extra queries at each save. And it's simply a custom field, which means adding it to a model is one line.
Usage:
class MyModel(models.Model):
name = models.CharField(max_length=50)
slug = AutoSlugField(populate_from='name')
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.
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.
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.
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!