I'm trying to use the Django nonrel project for google app engine. I setup the test project as described here. I added a new folder to the project named "static" for my static files. And for the app.yaml file i added the lines;
- url: /static
static_dir: static
I can't reach my static files. Do i have to do additional configuration?
Thx in advance.
Well i just figured it out. Just use static_dir line before the main.py. So the app.yaml should look like this;
As people already pointed out, you should put your
static_dir
directive before/.*
patternHowever, that is not the only thing you should know about.
By putting this directive into app.yaml, you make AppEngine webserver (whether it's development or production server) handle the path
/static
, and you need all the static files to be inside static directory. This means you will have to runpython manage.py collectstatic
every time you change anything in your static files (especially if you have/use apps with static files -- like, say,admin
ordjango-tinymce
) just to test these changes on local serverSo how to avoid that? By default staticfiles provides helpers to serve these files on development server without running collectstatic every time, the problem is the direcotry name conflict described in the previous paragraph: Django can't catch requests to your static files path, as they are handled by appserver. You can resolve it by using different paths on development and production server:
(djangoappengine sets DEBUG to True on development server). You can leave
ADMIN_MEDIA_PREFIX = '/static/admin/'
, but remember to run collectstatic at least once before using adminOf course remember to use
{{ STATIC_URL }}path/to.css
in templates instead of/static/path/to.css
Oh, and I assume that you distinguish the directory for original static files you work on and the directory where static files should be collected. I use this in my settings.py:
This means: you put your static fiels into
static
dir (and into your apps'static
dirs),collectstatic
collects them intositestatic
dir. Appropriateapp.yaml
directive isFinally, you can configure
app.yaml
to ignorestatic
andmedia
directories when uploading your app, since all the static files will be collected into and served fromsitestatic
. However, you should set this only while uploading (otherwise these files will not be available in debug server)app.yaml
have nothing to do with Django, but it does configures App Engine front-end. The answer depends on whether you want to serve static files with Django or the front-end (which is, well, cheaper and faster).If you just "added" your
- url: /static
mapping to the end, move it before the/.*
wildcard. As all mappings processed from top to bottom — first matching mapping wins.