Showing posts with label django. Show all posts
Showing posts with label django. Show all posts

Thursday, April 19, 2007

Using Mako in Django

It seems that Mako is one of the more powerful and faster template system for Python web development. In fact, Pylons is suppose to change in adapting Mako over Myghty soon. Since I'm using Django which uses its own template system, I was thinking I was out of luck. But fortunately, someone has written an adaptor for using Mako in Django!

I find it odd that there doesn't seem to be much talk about it. Maybe because it's only been out for a month or so. There isn't any documentation about it in his site, maybe he's just too busy. But I contacted him and he was quick in replying in how to getting it running. Thank you, Yi! So I want to help him out in return by writing a little guide on it so he doesn't have to answer annoying emails from newbies like me :)

Steps:
1. Download his files and save them in a path where you can import them, like in python2.5\lib or just under python directory if you're using cygwin. If you're sure you're going to use just Mako or Genshi (which is an XML template system, so it's slower, but has many devoted followers), you can combine the common.py and the one you're going to use into a single file to avoid filename conflicts of future libraries with common.py filenmae (or you can rename common.py to something else).

2. Follow instructions in installing Mako if it isn't installed already.

3. Add the following to your settings.py file of your Django project (here are my settings):

MAKO_TEMPLATE_DIRS = ('C:/Projects/mysite',)
# A tuple, specify the directories in which to find the mako templates,
# just like TEMPLATE_DIRS .
# default value is ('mako_templates',)

MAKO_MODULE_DIR = ''
# A string, if specified, all of the compiled template module files will be
# stored in this directory.

MAKO_MODULENAME_CALLABLE = ''

you can put these anywhere in the file I believe, I put them under the TEMPLATE_DIRS definition to keep all template stuff together. The TEMPLATE_DIRS definition by the way, will not be used anymore as you're now using Mako's. But leaving it defined to some path will not hurt.

4. In your views.py, add the following:

from mako_django import render_to_response

def some_function_named_in_urlpatterns(request):
return render_to_response('your_template.anything')

(I named it view_mktmpl.html, I believe the extension can be anything, but haven't really tried :).

5. Place your template file (your_template.anything), in where you specified in MAKO_TEMPLATE_DIRS. To learn more about writing a Mako template, see the Mako documentation.

hope this helps.


Time saver for Python web newbie developers

I recently begin learning Python web frameworks (I'm not a big fan of Ruby's execution speed) and did a bit of googling on them. Of the three most popular ones, namely Django, Turbogears, and Pylons, Django is by far the most popular of the three. According to both the wealth of information and google trends, Django is magnitudes more popular than the other two (yes, I know google trends doesn't filter out non web framework related results, but I believe it's still helpful). And it is easy to see why. Django has the best documentation, debugging information, and 3rd party help guides to help you get started. It's even got a second book coming out later this year and it's online now in form of a beta release.

After trying it out for a few days, I recommend Django for those who are new to web development. This is a recommendation only for newbies since I'm new to this myself. If you've been working with Django for a while, you may find it a bit "closed" according to many people. In fact, it is because of Django's reputation's of being a closed and non-flexible environment that I started learning Pylons first. (I did not look at Turbogears as much as all the information point out that the idea behind these two frameworks are very similar -- utilize the best of the other web framework components out there. Turbogears is said to be more mature and better documentation Pylons, but is in a flux of a major 2.0 change. Maybe I'll give it a try if Django doesn't work out.) Pylons seems to have only a tutorial, a cookbook, and a 3rd party Quick Site Guide. And that's it! There's not much more out there. So after spending three days on it, finding myself getting stuck more often than I like, and see no where I can go to expand my learning experience, I went over to Django and am glad I did!

References on my decision making:
Python web development and frameworks in 2007
Django and NIH
Python Web frameworks, Part 2 (scroll down to Conclusion)
TurboGears and Pylons (a technical comparison)
(There's many others, but as the title says, this is suppose to be a time saver for you, so I leave out the not too important ones)

Honorable Mention:
web.py - "the anti-framework framework"Reddit uses it.