Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Not that I know of. I did something like this using javascript and AJAX: when the title field loses focus, an AJAX request with it is sent to a url in you app that returns a slug. Here's the javascript:</p> <pre><code>&lt;script type="text/javascript"&gt; Event.observe(window, 'load', function() { form = $("post_form"); title = $('id_title'); slug = $('id_slug'); title.observe('blur', getSlug); }); function getSlug() { var titleText = $F(title); ajax = new Ajax.Request("/blog/utils/get-slug", { 'method': 'get', 'parameters': {'title': titleText}, 'onSuccess': setSlug }) } function setSlug(transport) { var slugText = transport.responseText; slug.value = slugText } &lt;/script&gt; </code></pre> <p>It uses Prototype but you could just as easily use jQuery or whatever. On the Django side, here it the view that the AJAX request is directed to plus some helper functions:</p> <pre><code>def GetSlug(request): """This Django view provides a url-friendly slug based on a given post title. It helps avoid primary key violations by creating different slug names for posts with the same title.It is designed to be called by an AJAX request from within the admin interface.""" title = request.GET.get('title', None) title = unquote(title) if not title: return HttpResponseNotFound("") result = findSlug(title) if not result: result = "" return HttpResponse(result) def findSlug(title): title = urlify(title) words = title.split('-') if len(words) &gt; 5: title = "-".join(words[0:5]) extraWords = words[5:] else: extraWords = [] def testSlug(slug): try: Post.objects.get(slug=slug) except Post.DoesNotExist: return slug return False #if slug does not already exist, return that if testSlug(title): return title #if title had extra words, add them one at a time and test that else: for word in extraWords: title = title + "-" + word if testSlug(title): return title #if we've run out of words and slug is _still_ not unique, try incrementing: title-2, title-3, etc. incrementor = 2 while incrementor &lt; 10: tempTitle = title + "-%d" + incrementor if testSlug(tempTitle): return tempTitle #if all else fails, punt to the user return None def urlify(string): """This takes a string (say, a post title) and convets it to something url-friendly &gt;&gt;&gt; title = "I read \"War &amp; Peace\" to-day" &gt;&gt;&gt; urlify(title) i-read-war-and-peace-to-day """ string = string.lower().strip() #remove leading, trailing whitespace string = string.replace("&amp;", "and") string = re.sub("[^a-z0-9- ]", "", string) #remove non-alphanumeric chars (except dashes and spaces) string = re.sub("\W+", "-", string) #replace whitespace with "-" string = re.sub("-{2,}", "-", string) #remove double dahses return string </code></pre> <p>The GetSlug view tries really hard to return something useful but unique, even in cases where the title might already exist for another record. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload