Posts Tagged ‘django’

django: Hacking django object_list to take list of templates

I recently found that you can pass a list or tuple to both django.template.loader.render_to_string and django.shortcuts.render_to_response. These functions iterate over the list of templates and render the first template that exists. The magic lies with the django.template.loader.get_template and select_template functions which take a single template name or list of templates respectively.
This is a great functionality [...]

django: How to get model class from model name

How do you get the model class object from a string containing model name?
This came up multiple times during SeenReport development. While recording views, saving votes and adding comment on an object, I had to get the model class using the model name. The scenario would be,

The view receives the model name and object id
Fetch [...]

django: How to expire session on browser close

django employs cookies to track the user session. settings.SESSION_COOKIE_AGE defines the session expiration age in seconds. So, the user logs in once and can stay logged in for hours (even days).
But what if a user wants the session to expire as soon as the browser is closed? This is perfectly desirable for users on public/shared [...]

django: generating absolute URLs

As I mentioned earlier, django provides a convenient and highly de-coupled way to manipulate URLs. At the heart of this functionality is django.core.urlresolver.reverse. This function however generates relative URLs e.g. /album/my-first-album/
At See’n’Report, we wanted every user to have a sub-domain so, hitting http://username.seenreport.com takes you to the user profile. But this created a small problem; all [...]

django: playing with upload_to field

Django provides models.FileField & models.ImageField to save uploaded files & images respectively. Both these fields take a parameters ‘upload_to’ which defines the path where the uploaded file will be saved (complete path is settings.MEDIA_ROOT + upload_to). You can play some neat tricks with this little feature.
Creating the date based folders at run time
You can use the [...]

django: using reverse instead of hardcoded URLs

django urls are a unique and neat way to abstract the urls from implementation. It is encouraged not to hard code the urls in templates to maintain abstraction. For templates, django provides a tag called url that you should use to render your urls. You can also name a url and refer to it using that name in this tag. [...]

django: Resolving “The model ‘MyModel’ is already registered” error

If you are migrating to django 1.0, youll have to update your models.py for django newforms-admin
Before

from django.db import models
class MyModel(models.Model):
"""
field definitions
"""
 
class Admin
[...]

django: extending User model at run-time

Any developer working in django must be familiar with django.contrib.auth.models.User class. It contains a set of basic attributes and methods. Fo small scale projects, these functionalities are more than sufficient. But most of the times you feel the need to store additional information about a user. django comes equippd with a mechanism to define an [...]

django: five mistakes that you are likely to make

django provides a convenient abstraction and relieves the developer of many internal (painful) details. But this causes the developer to fall into some subtle traps as well. Following is a list of common mistakes a django developer should watch out for.
1 objects.get(**filter_criteria) works for a single object
objects.get() is the easiest way to retrieve an object [...]

django: adding * to every required field

It is almost a standard to mark required fields on a web form with *. While working on django newforms (now forms) I realized quite late that the as_* methods of forms do not mark required fields with *. A straight forward way would have been to write an inclusion tag and just render the [...]