Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django auto slug in model forms like prepopulated-fields in django admin

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

like image 400
Llanilek Avatar asked Sep 07 '09 09:09

Llanilek


1 Answers

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.

like image 197
ozan Avatar answered Nov 15 '22 06:11

ozan