Posts de October, 2011

[Igor Sobreira] Replacing render_to_response with direct_to_template in Django

Wednesday, October 19th, 2011

Published on: 11/09/2010 20:29h

You certainly have already used render_to_response to render your templates, it’s a very convenient shortcut:

from django.shortcuts import render_to_response

def myview(request):

    ...

    return render_to_response('template.html', {'foo': ['one', 'two']})

At least until you need to use context processors, then you start writing code like this

from django.shortcuts import render_to_response

from django.template import RequestContext

def myview(request):

    ...

    return render_to_response('template.html', 

            {'foo': ['one', 'two']},

            context_instance=RequestContext(request))

this context_instance argument is the boilerplate code you will always need to make context processors work. I think it’s too much typing, and I’m lazy, so I prefer to use the direct_to_template generic view:

from django.views.generic.simple import direct_to_template

def myview(request):

    ...

    return direct_to_template(request, 'template.html', 

            {'foo': ['one', 'two']})

You don’t need the context_instance argument anymore, and you save one import.

But! There is one simple detail when passing extra context to this view, if a value in your dict is a callable, it will call and pass the result to template. It can be useful, but is something to keep in mind, if you’re trying to pass a callable to template.

Read the source code to understand how it works.

By Igor Sobreira

[Igor Sobreira] Django readonly fields in Admin

Wednesday, October 19th, 2011

Published on: 20/02/2010 09:22h

Finally Django has implemented readonly fields in Admin interface. It’s a new feature in 1.2 which is in alpha version at this moment, but will be released soon.

The feature is really simple and is already documented so I won’t explain how it works. But there is one implementation detail not documented that gives much more power to this feature. If you check the admin sources, you’ll find a method called get_readonly_fields() in django/contrib/admin/options.py, by default it just returns self.readonly_fields, which is the normal usage, but since you receive the request object as a parameter you can enable or not some fields depending on the logged in user.

Here is an example where a slug field can just be edited by superusers:

class FooModelAdmin(admin.ModelAdmin):

def get_readonly_fields(self, request, obj=None):

if request.user.is_superuser:

return ()

return (’slug’,)

There are lot’s of interesting new features in Django 1.2, I’m looking forward for it!

By Igor Sobreira

[Francisco Souza] Don’t rely on caching

Friday, October 14th, 2011

Don’t rely on cache. Cache doesn’t exist to make applications work, it exists to make them faster and/or more scalable.

[Francisco Souza] Speaking at PythonBrasil[7]

Friday, October 14th, 2011

Next weekend I’ll be talking about scaling Django applications at Python Brasil, the brazilian Python conference. It will be my first time at the conference, which is one of the greatest Python conferences in Latin America. Some international dudes are also attending to the conference.

[Francisco Souza] Creating HTML 5 slide presentations using landslide

Friday, October 14th, 2011

Recently I found landslide, which is a Python tool for creating HTML 5 slide presentations.It’s based in a famous slide presentation. It’s a simple script that generates HTML from a source file, which can be formatted using reStructuredText, Textile or Markdown.

[Francisco Souza] Splinter sprint on FISL

Friday, October 14th, 2011

We are going to start tomorrow, on FISL, another splinter sprint. “From June 29 through July 2, 2011, fisl12 will be hosted at the PUC Events Center, in Porto Alegre, Rio Grande do Sul, Brazil.”. But don’t worry about the location: anyone in anywhere can join us in this sprint. There is an entry in splinter wiki about this sprint, and I’m just replicating the information here…

[Francisco Souza] Testing jQuery plugins with Jasmine

Friday, October 14th, 2011

Learn how to test your jQuery plugins using Jasmine, a JavaScript BDD framework.

[Francisco Souza] Splinter: Python tool for acceptance tests on web applications

Friday, October 14th, 2011

Know Splinter, a Python framework for acceptance testing on web applications.

[Francisco Souza] Killer Java applications server with nginx and memcached

Friday, October 14th, 2011

Post showing how to configure a Java application server integrated with nginx and memcached.

[Francisco Souza] Using an exclusive Firefox profile for Selenium WebDriver

Friday, October 14th, 2011

Use a exclusive Firefox profile for Selenium WebDriver.