Posts Tagged ‘media’

Serving Static Media In Django Development Server

Monday, May 25th, 2009

There is a newer version of this post. Information below is no longer valid!


There is a misconception about how static files (a.k.a media files) are handled in Django. Actually it is quite clearly documented here and here. Nevertheless a question about this comes up in the mailing-list or IRC channel frequently:

Where do I put my media files?

Django can’t find my foo.gif!

How can I link my CSS?

First of all, just to make it clear; just because a server returns a response body with an internal URL doesn’t necessarily mean it will be available on that server. It is one thing that your templates produce the correct URL to a media file and another thing that your server actually serves that resource on that URL. Django development server doesn’t automagically serve media files1.

Settings

There are three settings to get right: MEDIA_ROOT, MEDIA_URL and ADMIN_MEDIA_PREFIX. MEDIA_ROOT is the absolute filesystem path where your media files are. I usually set it like:

MEDIA_ROOT = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'media')

This will set MEDIA_ROOT to point to the media directory in your project directory2. MEDIA_URL and ADMIN_MEDIA_PREFIX are URL’s:

MEDIA_URL = '/media/'
ADMIN_MEDIA_PREFIX = '/media/admin/'

With this setup, to serve admin media in production, all I need to do is to symlink media folder of admin app into my media directory. Of course you can set MEDIA_URL to point to another domain/subdomain. Such as http://media.mydomain.com/. But this way you can’t serve your media from development server.

URL Configuration

Add the following code snipplet at the end of your root urls.py:

   1 if settings.DEBUG:
   2     from django.views.static import serve
   3     _media_url = settings.MEDIA_URL
   4     if _media_url.startswith('/'):
   5         _media_url = _media_url[1:]
   6         urlpatterns += patterns('',
   7                                 (r'^%s(?P<path>.*)$' % _media_url,
   8                                 serve,
   9                                 {'document_root': settings.MEDIA_ROOT}))
  10     del(_media_url, serve)

settings.DEBUG == True doesn’t necessarily mean development server is running. But it is a good indicator since deploying with development server is not a good idea for many reasons. Notice here we don’t serve media unless MEDIA_URL is an absolute URL on our server.

Templates

Finally we need to specify media URL’s correctly. To avoid hard-coding media path we will be using {{ MEDIA_URL }} context variable in our templates. To have {{ MEDIA_URL }} included automatically in each template we need to do two things:

  1. Make sure you have django.core.context_processors.media in your TEMPLATE_CONTEXT_PROCESSORS.
  2. Make sure each view is using a RequestContext.

Afterwards all we need to do is to specify our media URL’s like this:

<img src="{{ MEDIA_URL }}img/header.jpeg" />

This will be translated to:

<img src="/media/img/header.jpeg" />

Bonus

While we are at it, why not serve our 500 and 404 pages statically. When DEBUG == True, 500 (server error) and 404 (not found) situations are handled with special debugging views. So there’s no chance to test your error pages. Add the following code, just like static serving code:

   1 if settings.DEBUG:
   2     urlpatterns += patterns('',
   3                             (r'^404/',
   4                                 'django.views.generic.simple.' \
   5                                 'direct_to_template',
   6                                 {'template': '404.html'}),
   7                             (r'^500/',
   8                                 'django.views.generic.simple.' \
   9                                 'direct_to_template',
  10                                 {'template': '500.html'}))

Now when you visit /500/ and /404/ on your development server you will be served a fake error page.


1: There is an exception here. If you configured your settings correctly, development server will serve admin media.

2: Assuming your settings.py is directly inside your project directory, hence the __file__.

Bookmark and Share

David Heinemeier Hansson at Startup School 08

Monday, March 9th, 2009

<div><a href="http://www.omnisio.com" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','http://www.omnisio.com']);">Share and annotate your videos</a> with Omnisio!</div>

Great talk. Also note the elegance of Omnisio player.

Bookmark and Share

Happy Birthday Fazlamesai!

Thursday, October 9th, 2008

Fazlamesai.net, the geek hub of Turkey, is now 8 years old. Happy anniversary, we love you!

Bookmark and Share

Social Networking Done Right

Monday, September 22nd, 2008

Let me just say that the social media, or more technically loser generated content is inherently flawed. That is of course a polite way of saying it is screwed. But I am not a polite person, so there you have it. I have found a CNET article explaining why. It is a short, good read. So I won’t go into details why social networking sites are crap.

But I would like to take the first argument from the article and elaborate on it a little bit; There’s nothing to do there (in a social network). Social networks like MySpace, Facebook and Orkut are based on creating a profile and associating other profiles with it. Thus you get to show everybody how many friends you have, you get to sell them and poke them and bite them (via Facebook applications), you get to rate them (in Orkut) and finally you get to send them private (and otherwise) messages. You can do all this stuff, so these social networks must be invaluable tools for our social life, right? Let’s take a closer look.

Last time I checked, relationships were more about quality than quantity. Who cares if you have 10 or 1000 friends in your profile? Having a huge friend count doesn’t even make you a popular person as far as I’m concerned. Because I know people who just pop out of nowhere and request to be friends with me. Unsurprisingly they have a high friend count themselves, go figure!

I think relationships are about sharing experiences. Social networks like Facebook give you that opportunity. In a twisted sick way though. For example you can poke your friends. Think about how much you can strenghten your relationships by poking people. And not only that, thanks to the application API you can buy and sell your friends as pets or you can suck their blood till they become vampires. Just the constant stream of invitations makes up a great experience. But I doubt it is the kind of experience Facebook wants you to have.

But hey, you can use social networks to communicate with people too. Isn’t that a good thing? You can publish your status, you can send your friends private messages. You can even join groups to meet like minded people! Isn’t that cool? …well, no. If you think that’s cool you must have missed the news about something called Internet! You can do all these without a social apparatus and almost always more effectively. You can email people for example. I would suppose people check their email more often and with greater attention than their social web2.0 gadgetry Mainstream social networks are not much more than profile association tools. There is nothing to do there. And noone, other than your friends, cares about your profile decoration. Perhaps not even your friends…

This huge mass of loser generated content reminds me of all that wasted bandwidth over once popular and useless e-mail forwards. When I lashed out to the senders they would be offended and surprised at the same time. How could I reject these wonderful delights Internet has to offer us. Do they still forward? That was before we had hyper-super-wall applications and send-poop applications. Actually I am a big fan of user generated content. There are many blogs for example, not only worth reading but their content is so precious that you can’t just possibly buy a book or take a course to get to that information. These people genuinely have something to tell and they have spent the effort to set-up a proper channel for their valuable voice. There are lame blogs as well, but of course if you’re reading this you already know that. But the bad ones are not strongly connected with the good ones, therefore you don’t even have to notice them. In other words they can not publish stories in your news feed.

There are also social networks that are built around another application. So there is a common goal, or at least something solid to talk about. I divide them into three and a half categories; building, sharing, bookmarking and business.

  • Building applications with social networking are in my opinion most sophisticated and most valuable. Common example is Wikipedia. If you find it hard to spot social networking elements in Wikipedia that is probably because you have only seen the frontend. Wikipedia is a big community, and they form a social network with a wide communication bandwidth. Just google it to find out about how they operate and edit. Another good example to this category is open source software development. They also form a social network, and the networking aspect is much bigger this time.
  • Sharing applications with social networking include Youtube, Flickr, 8tracks and the like. I won’t deny most of the content here is loser generated, just go to a random Youtube video and try to read the comments. But that may be, on an end user level, irrelevant. In these applications networking is not pushed too hard and the sharing mission is fully accomplished.
  • Bookmarking is actually a special case of sharing. Bookmarking applications such as Reddit, Digg and Stumbleupon are built on an incredibly powerful idea of sharing bookmarks. Today we are using WWW for many different things, but generally surfing is still the most prevalent. And hyperlinks and search engines fail to serve well enough for general purpose surfing. Social bookmarking does. You can choose a general or specific category and enjoy an almost endless stream of human reviewed websites.
  • Business oriented social networks form half a category. They are not very different than mainstream friend portfolios. They emphasize business networking and I hear they can be useful. People are more open to meet new people in business context and these sites copy real world interaction successfully, so I don’t put them together with the other time wasters.

If you want a web presence the best way in my opinion is blogging. If you don’t have anything to say, no matter how many social profiles you have and how many times you can twit a day doesn’t really matter, nobody cares. You can also incorporate social media if you like, you can import your RSS into your Facebook profile or ping.fm some your posts. There is no need to spend a lot of time tweaking your profile, it is pointless.

On the other hand social networking is an important concept. It adds value when it is built around another, related service. Take Kongregate for example. I mentioned about how it is structured as a game which forms social network with unique dynamics. Check it out, if you also believe profile association is not the highest point for social networks.

Bookmark and Share

Omnisio

Friday, August 22nd, 2008

Omnisio is a video hosting application, one of Y Combinator startups, acquired last month by Google.

Looking at aqcuisitions like this one, I see two things;

  • Improving end user experience (or conversation in some cases) is more important than technical superiority. Omnisio didn’t host uber quality HD videos or 3D sound or anything fancy like that. What made me fall in love with the service is that they simply synchronized (vector) keynotes with the actual video.
  • Execution is king for investors of any kind. This should be no surprise though. Not because they risk their money. But because startup businesses are usually making up their specific business as a whole as they go. So there is not really any substantial know-how, unless you are a copycat.

Check out Startup School 2008 videos on Omnisio.

Bookmark and Share