How to serve uploaded media in Django

Posted on April 25th, 2011 in django | No Comments »

I recently started to learn Python / Django.

I had been happily using the staticfiles app in order to serve images and javascript files which were used in my page layout.

However, I soon ran into problems when I started working with user-uploaded files. The files had been uploaded to the correct location, but I could not access them from the browser.

The solution to this problem is simple. To serve user uploaded content on your local machine or development server, just add the following to the end of your project’s urls.py file:

from django.conf import settings

if settings.DEBUG:
    urlpatterns += patterns('',
        url(r'^media/(?P
.*)$', 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,
        }),
   )

Just remember to provide a suitable alternative (for example a separate nginx server) for accessing them once your project goes live!

Prevent Pagerank Splitting with 301 Redirect

Posted on July 18th, 2010 in Code snippets, SEO | 1 Comment »

It is generally considered good practice that your website should be available via ‘yoursite.com’ and ‘www.yoursite.com’. Unfortunately, when someone else links to your site, you have no control over the format of the URL they use. This inconsistency leads to the possibility of search engines treating the URLS as two separate websites.

Google’s Webmaster Tools actually allow which format of the URL you prefer to be used, however in order to make sure that the same problem does not occur with other search engines you can use a 301 redirect to whichever format you prefer. Personally, I prefer to use the non-www format.

In order to this, simply add the following to your .htaccess file or the relevant part of your Apache httpd.conf file:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

You can check the status of your site using the no-www site.

List of freely available programming books

Posted on June 24th, 2010 in Resources | No Comments »

Whilst looking for something else on stackoverflow, I came across this thread.  It’s basically an extensive list of free, online programming books!

PHP Ternary Operators

Posted on June 13th, 2010 in Code snippets, PHP | No Comments »

PHP’s ternary operators (operators with three operands) can be used as a shorthand for performing basic if-else statements.

Read the rest of this entry »

Eight Essential Tools for Every Web Developer

Posted on June 10th, 2010 in Development | No Comments »

Here is a list of useful tools I use on a regular basis that I think every developer should know about.

Read the rest of this entry »

Alternating table row background colour with PHP and CSS

Posted on May 26th, 2010 in Code snippets, PHP, Tutorials | No Comments »

If you’ve ever dabbled with Ruby on Rails, you may have come across the cycle helper, which is used to return alternating values whilst iterating over an array.

This is an effect I often need to recreate in most projects I work on.  As a result, I came up with this little helper function.  It’s not quite as elegant as the Ruby version, but it gets the job done.

Read the rest of this entry »

Passing arrays through HTML forms

Posted on May 21st, 2010 in Code snippets | No Comments »

Working with forms can be a pain. Rather than giving each field in the form a completely separate name, it makes sense to group them into a corresponding group of elements. This is easily achieved by using your form fields to create an array.

Read the rest of this entry »