summaryrefslogtreecommitdiff
path: root/published/Webmonkey/Monkey_Bites/2007/01.08.07/Tue/generic-views.txt
blob: 7d74c7abadd32f705c5d7b55ad270fadf706f2d7 (plain)
1
One of the great charms of Django is the amount of work it does for you. It's literally possible to build complex applications that mirror the features of say, WordPress, in little more than a hundred lines of code.

How the heck does Django do it? Well one of the great tools that Django puts at your disposal is something called generic views. Views are simply python functions that get called whenever a browsers requests a page. 

There's a myriad of ways to storing and retrieve data in a web application, but date-based structures are pretty common. For instance, the url of this page contains something like '.../2007/01/...' which is a date-based archive.

Rather than requiring that you write your own code every time you build a site that uses date based archives, the designers of Django included some generic views to handle common cases. In this case, were this site powered by Django, which regrettably it is not, but were it, we could pass the date from the url to django in one easy like of code:

	(r'(?P<year>\d{4})/(?P<month>[a-z]{3})/$', 'django.views.generic.date_based.archive_month', data_dict),
	
Okay maybe it looks confusing, but it's really not. The first part is just a regular expression that captures the url and passes it to the generic view <code>archive_month</code>. The last bit is a python dictionary which would tell Django what data model to use for this page.

Another common task web applications perform is displaying a list of content and to this end there is Django generic view for lists.

Which brings us to today's tutorial. Generic views are great, but what if they don't exactly fit your application? Well, it's pretty easy to extend generic views and James Bennet [has a great tutorial on his blog The B-list][1] that runs through the basics extending, tweaking and otherwise making generic views do what you want.

Mr. Bennet also has a number of other very useful and easy to follow django tutorials, try [digging through the rest of his Django archives][2].

[1]: http://www.b-list.org/weblog/2006/11/16/django-tips-get-most-out-generic-views "Django tips: get the most out of generic views"

[2]: http://www.b-list.org/weblog/categories/django "The B-List: category: Django"