Posts Tagged ‘django’

Python Web Frameworks

Sunday, August 15th, 2010

I wanted to write about this for sometime. DISCLAIMER : I am inexperienced in this, so I might be wrong

Long time ago (approximately when Turbogears 2 was released) I decided to learn Python. I saw Mark Ramm’s talk at Djangocon and decided not to pick Django as a framework to learn. I tinkered with Turbogears 2, nothing big, just learning how things work. I stopped to learn Python and started to use it as my preferred scripting language. During this time I also took interest finding other Python web frameworks or components, learning their pros and cons without actually trying it (there are so many python web stuff out there). This weekend, I browse around finding any updates in Python web stuff since I didnt keep myself up to date. I just knew that Pylons hit 1.0 and there is a Flask out there for example.

Django
I have been using this for a few months now. It is really easy to pick up. Most of the times, Django will be enough to fulfill your Python web framework needs. A great amount of users is a big help. You may find some problems only to find that someone already experienced it and asked for help. In stackoverflow, cmiiw, there is more questions with Django tag than other Python frameworks combined.

Turbogears
Now I think they have the killer app. The much-more-than an admin app. If I remember correctly there was this application back then, but now it’s different. I dont know much about the technical detail but from what I read, it is really promising. Here’s from the docs

TurboGears Admin can be utilized to support more than just Administrative tasks. Since it is secured the same way the other TurboGears controllers on, you could use it for any user on your system. The myriad of ways you can override different parts of the system mean that this tool could be an excellent resource for rapid prototyping of a web application, or even as a provider of placeholder for future components.

and here’s from the Sprox creator

When I’m done, you will be able to easily swap between nothing, dojo, and jquery, and mootools support, or mix and match at any level. You can also switch between mongo, or any rdbms SA supports, and hopefully couchdb soon. It’s kinda scary that’s true.

It is scary! I havent tried it yet but it sounds better than the Django admin. This is enough reason for me to learn Turbogears again. It will be kinda hard since Im kinda familiar with Django now.

Nested Archive Link in Django

Wednesday, June 9th, 2010

Let’s say you have a blog in Django and want to have archive link on the sidebar. The link of course is matched to the date of your posts. You cant have a ‘June 2010′ link if you never post on that month. This post will try to handle that, creating nested archive link yearly and monthly.

data = Post.objects.all()

Here you got a QuerySet containing all your posts. Next is getting distinct year/month values from your posts grouped by year. To do this you need a set of distinct years.

year = ([d.year for d in data.dates('publish', 'year', order='DESC')])

Now that you have it, you can get what you want. I originally used a dict to contain the result,

result = dict([(y, data.filter(publish__year=y).dates('publish', 'month', order='DESC')) for y in year])

but dict is not sortable (you can sort the keys on an external data structure, though), so I put it on a nested tuple.

result = ([([y, data.filter(publish__year=y).dates('publish', 'month', order='DESC')]) for y in year])

I use date_based generic view, so I put this on extra_context for easier inclusion. All that remains is viewing it on the template with filters and stuff. I am pretty new with this, any better suggestions?