How do I serve static files using django & PythonAnywhere?

[update 2014-08-27] -- this guide is out of date. Please refer to the wiki entry on static files instead.]

Static files are the bits of your web site/application that are not created on the fly by Python. They might be images you want to embed, css files for styling, or even a PDF that you want people to be able to download.

If you use PythonAnywhere's Django quickstart then we already provide some sensible defaults which will get things working without you having to do anything. If however you are migrating an existing site or want to set things up manually then I will walk you through the process.

Out of the box Django needs static files for the admin interface. If you visit www.yoursite.com/admin and it doesn't have any styling then this is the problem. Django looks for these static files at www.yoursite.com/static/admin. So under the web tab of your PythonAnywhere dashboard you need to select the tab for your site and add your first entry for static files.

The url should be:/static/admin/

The directory (or path) should be: /usr/local/lib/python2.7/dist-packages/django/contrib/admin/media

Next you probably want to serve some css or images out of a folder called static in your project folder. If your username was eliza and your Django project was called suitcase the following setup would work for you.

The url should be: /static/

The directory (or path) should be: /home/eliza/suitcase/static.

You resulting static files settings should now look something like this. Of course your username and project name should be different. That's the "eliza" and "suitcase" part of the path.

PythonAnywhere static files

Any sub folders are also searched. So you could put your css files in a folder at /home/eliza/suitcase/static/css and a visitor going to http://www.yoursite.com/static/css/my.css would be sent the file /home/eliza/suitcase/static/css/my.css.

As always, remember that you will need to reload your web app before these changes actually take affect.

Happy Pythoning.

[update 2014-08-27] -- this guide is out of date. Please refer to the wiki entry on static files instead.]

blog comments powered by Disqus

PythonAnywhere is a Python development and hosting environment that displays in your web browser and runs on our servers. They're already set up with everything you need. It's easy to use, fast, and powerful. There's even a useful free plan.

You can sign up here.