Once Upon a Time, There Was Nothing (part. 2)
One of the main reasons I like Django is it’s awesome auto-generated admin interface. It’s just awesome. When you start to play with it and discover apps like django-admin-tools, the faded django-jqmobile and other cool stuff you realise how versatile Django really is.
When I started learning Django I created a small manager for some of my hobbies. The first one was a book manager in which I have a log of every single book I’ve ever read, when I read it, where the book is located, if it’s loaned to somebody, a “GIVE IT BACK TO ME" button that sends an email with that exact message to whoever has it, and so on. Among everything, I also added a field for the book’s cover but I never really used it.
More recently, I restarted to improve my card deck collection (yes, I’m weird) and added this new feature to my Django manager. But this time, since it’s cataloging a collection and decks are mainly visual, the photos of the decks are very important. Yes, I said imageS. Each deck has five photos:
- The Front of the Box
- The Back of the Box
- The Back of the Cards
- The Joker
- The Ace of Spades
There are decks with a different back for each card, but that’s not important,. Those are the options I’m contemplating. But, since not all of them are opened (I have one numbered deck that goes to 2500 copies. I won’t open it. Never!) only the first two images are requirements.
So, now that you are aware of the context, this is when the problem (and the fun) started.
I started looking for some Django solution that could easily show thumbnails for my images in my admin module. Guess what was my surprise when I realised there was None.
There was absolutely nothing that would do what wanted and needed. The closest things I found were the automatic thumbnail creator sorl-thumbnail and Ace Devs solution which didn’t solve my problem. Dude, my images have 1936x2592 pixels of resolution. I can’t just show them as they are.
That’s when I realised I would have to develop a new solution for that.
The Solution (aka The Fun Part)
I’m a modest guy, I don’t like bragging about what I do.
No forget it. Bullshit. I do like to show everyone what I did. Specially when I think it’s awesome, I mean, It’s awesome. Don’t you like sharing awesome stuff with your friends? Dude, I’m even writing an article about it! In TWO languages!
So, what I did was basically to join and improve those two solutions I found. “Basically" because, in the deep, it was a little more complex than that.
I came up with the stupidest yet simplest name I could find: django-admin-thumbnail. I know, it’s über stupid. But it’s also simple, so it works.
The package automatically works on your list_display of your ModelAdmin. It automagically detects your ImageFields, converts it to thumbnail (physically allocated) using sorl-thumbnail and displays to you in a user friendly way.
This is how it looks when you hover your cursor over an ImageField in your model grid.
The image is in portuguese. Imagem = Image, Abrir Imagem = Open Image. django-admin-thumbnail is localizable but,
en-US are the officially supported languages. If you want it to be in your language, just
send me your
.po file and I’ll validate it and share. It’s ridiculous, there are only two tags. Give it a try!
When I finished, I realised how simple and effective it was. I had to share it with the community. So I took the
opportunity to create my first
python package. It was a pain in the ass because the documentation is lame, specially
the long-description part. I had to generate it a zillion times to get the reStructured Text working until I found
the py2html command.
Wrap and Finish
That’s it. If you want to know more about django-admin-thumbnail just click the link and it will send you to the Github repository. There’s a quick start guide. Here’s the link for the python package.
If you have any trouble to use it, don’t hesitate to send me a message.