Django Settings Site Domain example.com

It took me a while to figure out how to change from the default, example.com. Maybe it should have been obvious, but I was looking in all the wrong places and every search I did wouldn’t come up with an answer. As I discovered, it isn’t a setting in the settings.py file. It’s something that is in the database. You can change it through the Django admin interface, phpMyAdmin, or however you feel comfortable. It’s in the django_site table. When setting SITE_ID in settings.py it is the ID in this table. And this is the information that Site.objects.get_current().domain uses. Which is awesome for rss feeds and e-mails that you send out or anytime you need the domain in a link.

from django.contrib.sites.models import Site
domain = Site.objects.get_current().domain

Django RequestContext Example

Browsing other peoples’ code is a great way to learn new things about a language or framework. I never made it to the Django docs about Contexts, but the Pinax developers apparently did and I got a chance to learn this from them. This is a few sections of their code and how they use RequestContext in their apps.

If you are looking at the source of some of their views you might see how they are using it. Here is what it looks like in friends_app.friends

    return render_to_response(template_name, {
        "join_request_form": join_request_form,
        "invites_received": invites_received,
        "invites_sent": invites_sent,
        "joins_sent": joins_sent,
    }, context_instance=RequestContext(request))

So what extactly does context_instance=RequestContext(request) do? I took a look at the django documentation to find out more. And that led me to check the settings file and I found that there were quite a few things listed in TEMPLATE_CONTEXT_PROCESSORS.

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.core.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.request",
 
    "notification.context_processors.notification",
    "announcements.context_processors.site_wide_announcements",
    "account.context_processors.openid",
    "account.context_processors.account",
    "misc.context_processors.contact_email",
    "misc.context_processors.site_name",
    "messages.context_processors.inbox",
    "friends_app.context_processors.invitations",
    "misc.context_processors.combined_inbox_count",
)

I opened up friends_app.context_processors to see a bit more and it looked like this

from friends.models import FriendshipInvitation
 
def invitations(request):
    if request.user.is_authenticated():
        return {
            'invitations_count':
            FriendshipInvitation.objects.filter(
            to_user=request.user, status="2").count(),
        }
    else:
        return {}

This means that every view that has context_instance=RequestContext(request) in it will call the above function since it is listed in settings.py and it will provide the template variable, {{ invitations_count }}.

Using RequestContext makes it easy to have the common template variables available on every page and I will have to start using it more in my apps. So make sure you have from django.shortcuts import render_to_response and from django.template import RequestContext in your file and add your context processor to the settings file and you should be ready to add template vars to your contexts.

Setting up Apache2, mod_python, MySQL, and Django on Debian Lenny or Ubuntu Hardy Heron

Both Debian and Ubuntu make it really simple to get a server up and running. I was trying a few different Machine Images on Amazon and I found myself repeating a lot of things so I wanted to put them here for reference for those who might find it useful.

With a fresh image, the first thing to do is update apt-get.

apt-get update && apt-get upgrade -y

Then grab all of the software to use.

apt-get install -y xfsprogs mysql-server  apache2  libapache2-mod-python  python-mysqldb  python-imaging  python-django  subversion php5  phpmyadmin

xfsprogs is for formatting an Elastic Block Store volume and may not be needed in all cases.

I like to check out the latest version of Django from their repository, it makes it easier to update it later. This also starts a project named myproject (this name is used later).

cd /usr/lib/python2.5/site-packages
svn co http://code.djangoproject.com/svn/django/trunk/django django
ln -s /usr/lib/python2.5/site-packages/django/bin/django-admin.py /usr/local/bin
cd /var/www
django-admin.py startproject myproject

Now to edit the apache config to tell it about our project.

cd /etc/apache2
nano httpd.conf

Add the following to set up python to run the django files and php to run the phpmyadmin files. There is also an example of serving static files. Change where it says myproject if you used a different name.

<Location "/">
    SetHandler python-program
    PythonHandler django.core.handlers.modpython
    SetEnv DJANGO_SETTINGS_MODULE myproject.settings
    PythonOption django.root /myproject
    PythonDebug On
    PythonPath "['/var/www'] + sys.path"
</Location>
 
 
Alias /adm_media/ /usr/lib/python2.5/site-packages/django/contrib/admin/media/
<Location "/adm_media/">
    SetHandler None
</Location>
 
Alias /files/ /var/www/myproject/files/
<Location "/files/">
    SetHandler None
</Location>
 
Alias /phpmyadmin/ /usr/share/phpmyadmin/
<Location "/phpmyadmin/">
    SetHandler None
</Location>

Restart apache for it to use the new configuration.

/etc/init.d/apache2 restart

The only thing left to do is set up the database. If Ubuntu had you set up a root password already, add -p to the end of the following command to use it.

mysql

There are some users in mysql without username, it is best to remove those.

drop user ''@'localhost';

Do that for each host that has a blank username. Use the following to see all users.

SELECT user, host FROM mysql.user;

Create a database and add a user.

CREATE DATABASE db_name;
GRANT ALL ON db_name.* to user_name WITH GRANT OPTION;
SET PASSWORD FOR user_name = password('psswdhere');

If root doesn’t have a password yet, use the above commant with root as the username.

Amazon has a page about how to use EBS with MySQL, but there are reported issues with using Debian Lenny and EBS.

Static Files in Django on Production and Development

Update 2009-03-25

I realize why this isn’t needed. If your production environment is set up correctly, django will never serve the static urls and you aren’t running the django development server anyways, so having django.views.static.serve in your urls file won’t hurt anything. But it is nice to have if multiple people will be developing this on different machines.

Original Article

For developing a django application, I’ve been using the webserver that comes with django. It’s great for development since there is no need to restart apache. The quick and easy way to server a static file with the django webserver is to use this in your urls.py:

urlpatterns = patterns('',
    (r'^files/(?P<path>.*)$',
        'django.views.static.serve',
        {'document_root': '/home/django-proj/files'}
    ),
# ... more url patterns
)

Which worked great for testing things out, but it is definitely not a good idea to use this in a production environment. On the production server I set apache to not bother with mod_python for those directories so that django doesn’t have to do any processing (here is a thorough example of using lighttpd to serve static files).

Since I wanted to use the same files on each server, I didn’t want the static serve in the url patterns on production. To add more patterns to urlpatterns, you can do urlpatters += patterns(”, to add more. I also added the file locations into settings.py since they would be different for other developers. Using if hasattr(settings, ‘STATIC_FILE_LOCATION’): I was able to keep the urlpatterns in the right format whether it’s in a production or development environment.

An example of what I’m using:

from django.conf.urls.defaults import *
from django.conf import settings
 
# basic urls
urlpatterns = patterns('',
    (r'^logout/', 'user.views.logout'),
    (r'^login/', 'user.views.login'),
)
 
# if on a development machine
if hasattr(settings, 'STATIC_FILE_LOCATION'):
    urlpatterns += patterns('',
        (r'^files/(?P<path>.*)$',
        'django.views.static.serve',
        {'document_root':
        settings.STATIC_FILE_LOCATION}),
    )
 
# application urls
urlpatterns += patterns('',
    # add new applications below here
    (r'^user/', include('user.urls')),
    # end applications
 
    # the index
    (r'^$', 'user.views.index'),
)

Quick Thumbnails in Django

I normally like to write code myself instead of installing some large script just to do one task for me. There were a few scripts out there that could create thumbnails, but I wanted something simple and wouldn’t use most of those features. Plus, I wanted to know how to use the Python Image Library with Django 1.0 and learn on my own how to take an uploaded picture and create a few thumbnails of them.

After searching for a while I was able to piece some things together to get something working. In my model I added these two functions.

def thumbnailed(self, file, width, height):
    from django.core.files.base import ContentFile
    from StringIO import StringIO
    import Image
    try:
        size = width, height
        tmp_file = StringIO()
        im = Image.open(StringIO(file.read()))
        format = im.format # since new im won't have format
        if format == "gif" or format == "GIF":
            im = im.convert("RGB")
        im.thumbnail(size, Image.ANTIALIAS)
        if format == "gif" or format == "GIF":
            im = im.convert("P", dither=Image.NONE, palette=Image.ADAPTIVE)
        im.save(tmp_file, format, quality=95)
    except IOError:
        return None
 
    return ContentFile(tmp_file.getvalue())

Using StringIO I was able to create a temporary file in memory to hold the thumbnail data and return it where it would later be passed to django. I was trying to create 3 thumbnails which presented another problem: django was obliterating the uploaded file and the data in my temp files when I was saving. So I figured out how to get around that, but there may be better ways.

def create_thumbnails(self, file):
    pic = self.thumbnailed(file, 160, 400)
    if pic is not None:
        self.picture.save(file.name, pic, save=False)
 
    # Django's InMemoryUploadedFile uses StringIO
    # This will reset it to be ready to use again
    file.seek(0)
 
    thumb = self.thumbnailed(file, 288, 96)
    if thumb is not None:
        self.thumbnail.save(file.name, thumb, save=False)
 
    # Django's InMemoryUploadedFile uses StringIO
    # This will reset it to be ready to use again
    file.seek(0)
 
    tiny = self.thumbnailed(file, 144, 48)
    if tiny is not None:
        self.tiny_image.save(file.name, tiny, save=False)

The model has picture, thumbnail, and tiny_image as ImageFields and create_thumbnails was originally called from the view and passed the uploaded file from request.FILES.

There was a lot of trial and error trying to get this together, so I hope it helps someone get past that.

Updated July 10, 2009

I added a condition to check if the image is a gif. If it is, it is converted so that the thumnails will look much better than they would without converting them. I also set the quality to 95 so that all images will have the best possible thumbnails.