可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am running GoogleAppEngine (GAE) 1.6.3 with Python 2.7 and Django 1.3 by having:
libraries:
- name: django
version: "1.3"
in my app.yaml
. The following should serve the admin media files at url /static/admin
:
- url: /static/admin
static_dir: django/contrib/admin/media
expiration: '0'
But I get 404s for such admin media (css, etc). Am I using the correct location for the Django admin's media file?
回答1:
The best way to do this is to copy or symlink the media
directory into your app directory in your local files, so it is uploaded with your app's files. Then your app.yaml
can refer to the relative path in the app directory.
There is a $PYTHON_LIB
variable substitution you can use in app.yaml
paths, but it looks like Django is not under $PYTHON_LIB
in the live version of the Python 2.7 runtime.
回答2:
When adding this to app.yaml
handlers:
- url: /static/admin
static_dir: static/admin
expiration: '0'
I was able to serve the CSS files by:
Adding this to settings.py
:
BASE_DIR = os.path.abspath(os.path.dirname(__file__)) + os.sep
STATIC_ROOT = BASE_DIR + 'static'
And then running
python manage.py collectstatic
The admin media files appear correctly locally as well as on appspot.com.
The last command copies the media files into the the static/
directory. So in fact does what Dan Sanderson suggested but in a more automated way.
回答3:
I tried Philipp Keller's collectstatic
, but I don't have that command available.
So, add this handler to app.yaml
:
- url: /static/admin
static_dir: django/contrib/admin/static/admin
expiration: '0'
then, in settings.py
, delete ADMIN_MEDIA_PREFIX
(removed in django 1.4) and add:
STATIC_URL = '/static/'
and you have working css.
回答4:
is possible static file referenced by variable $PYTHON_LIB on deploy ??
file app.yaml
application: hello
version: 1
runtime: python27
api_version: 1
threadsafe: true
libraries:
- name: django
version: "1.3"
handlers:
- url: /admin/media
static_dir: $PYTHON_LIB/lib/django_1_3/django/contrib/admin/media
builtins:
- django_wsgi: on
log local:
INFO 2012-04-03 02:06:19,200 dev_appserver.py:2884] "GET /admin/media/css/base.css HTTP/1.1" 200 -
INFO 2012-04-03 02:06:19,207 dev_appserver.py:2884] "GET /admin/media/css/dashboard.css HTTP/1.1" 200 -
INFO 2012-04-03 02:06:19,242 dev_appserver.py:2884] "GET /admin/media/img/admin/default-bg.gif HTTP/1.1" 200 -
log error deploy app:
2012-04-02 19:17:32.775 /admin/media/css/dashboard.css 404 6ms 0kb
- [02/Apr/2012:19:17:32 -0700] "GET /admin/media/css/dashboard.css HTTP/1.1" 404
Static file referenced by handler not found:$PYTHON_LIB/lib/django_1_3/django/contrib/admin/media/css/dashboard.css
回答5:
Following seems to work fine for me.
app.yaml
handlers:
- url: /static
static_dir: staticfiles
settings
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
Run python manage.py collectstatic
. Now under your staticfiles admin folder should be created.