views:

653

answers:

1

is there a way to get the same results of using pre-populated fields in django's admin site for slug fields in a standard modelform

+2  A: 

Well django is open source, so if you want to replicate certain behaviour you can read the code and pick and chose what you like. For instance, you can see that contrib.admin uses a script called urlify.js to do the dynamic slugging, with usage something like this:

<script type="text/javascript" src="/admin-media/js/urlify.js"></script>
<script type="text/javascript">
document.getElementById("id_title").onkeyup = function() {
    var e = document.getElementById("id_slug");
    if (!e._changed) { e.value = URLify(document.getElementById("id_title").value, 50); }
}
</script>

... depending of course on where your admin media is served from (mine is from "/admin-media/")

Or if you're happy to do your slugifying in your view, you can use the function that's used in django.template as the slugify filter: django.template.defaultfilters.slugify.

ozan
thanks the slugify method works great! exactly what i was looking for.
Neil Hickman