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 [...]
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 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 [...]
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 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 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.core.mail.send_mail provides a very convenient abstraction to send an email to multiple recipients. But calling send_mail is a blocking call. This means that if you want to send an email in response to a user action, the server will not respond till the email is sent. This problem is magnified when you have to send [...]
I have been a great admirer of simplicity of django. So I was rather disappointed when I saw the newforms-admin. Using newforms-admin requires more code, more named classes and more work . This is a simple comparison of how to use admin functionality before and after the changes.
Before
#file models.py
from django.db import models
class MyModel(models.Model):
[...]
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
[...]
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 [...]