I am a bit of a novice when it comes to Django and Google App Engine, and I've been having trouble setting up project paths in my app settings. Specifically, I'm trying to reference a CSS file for use with a template. My directory structure is as follows:
app/
app/
-'__init__'.py
-settings.py
-urls.py
-views.py
-wsgi.py
img/
static/
css/
-style.css
templates/
-index.html
I think my issue lies in the settings based on the work I've been doing for the past few hours. I started being unable to reference a template, though I figured out how to do that, and let me explain how:
In settings.py, I have the following pieces of information regarding templates:
PROJECT_PATH = os.path.abspath(os.path.dirname(__name__))
TEMPLATE_DIRS = (
PROJECT_PATH + '/templates',
)
This turned out to be simple enough. I set a PROJECT_PATH which holds the path for the project, and appended the templates directory. However, I'm still having trouble with doing the same for static files, and this is where I'm kind of lost. With regard to static files, I have the following in settings:
PROJECT_PATH = os.path.abspath(os.path.dirname(__name__))
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
PROJECT_PATH + '/static',
)
Theoretically these default values should work fine as long as the directory I'm using is named '/static/' but it doesn't work the same way as templates. I've tried quite a few different things to try to get these values to point to the correct directory, but only one time have I actually been successful, and I think that was a combination of using absolute paths i.e 'Users/username/app/static/css/' which don't make sense once I deploy the project because I am not hosting it on my computer. Also I probably had moved the static directory inside the app subfolder, which should also be unnecessary. What is different with the static files compared to the template files? Why can I use the PROJECT_PATH and append '/static' to achieve the same result as with the templates?
Some other useful information from my project:
app/urls.py:
urlpatterns = patterns(
'',
url(r'^$', views.index, name='index'),
) + staticfiles_urlpatterns()
and:
templates/index.html:
<html>
<head>
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}" />
</head>
<body>
<!--Some stuff-->
</body>
</html>
and:
app.yaml:
application: siren-1157
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: main.app
- url: /static
static_dir: static
libraries:
- name: django
version: "latest"
EDIT : Based on some feedback from @AlexMartelli I've added application_readable: true to the app.yaml, in this way:
- url: /static
static_dir: static
application_readable: true
Some other resources I have used while troubleshooting, with ill effect:
Django -- Can't get static CSS files to load https://docs.djangoproject.com/en/1.4/howto/static-files/#configuring-static-files https://github.com/divio/django-shop/blob/master/example/settings.py
Some other things I've tried:
Change reference technique in index.html:
from
{% static 'css/style.css'}
to
{{ STATIC_URL }}css/style.css
Changed PROJECT_PATH with a variety of different os modules
Changed STATIC_ROOT, STATIC_URL, and STATICFILES_DIRS to various different values, from absolute path ('/Users/username/app/static/') to a relative path using PROJECT_PATH or a derivative
Is anyone able to point me in the right direction with figuring out how to set up directories? I feel like I'm close but it's driving me nuts right now.