4

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?

Joseph Victor Zammit
  • 14,760
  • 10
  • 76
  • 102

5 Answers5

3

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.

Dan Sanderson
  • 2,111
  • 12
  • 12
  • Thank you for your reply, I have your book too, very good book though a bit outdated today. So there is no way to set a GAE static file handler to point to the Django admin media on Live? I've sorted it out the usual way locally, Live is the problem. So I have to upload all Django Admin media files, correct? I thought I could avoid this. – Joseph Victor Zammit Mar 26 '12 at 08:56
  • Had to copy all the files and place them with rest of the files for upload. Admin static files alone are 640Kb. That's more than half of my whole application! Any plan to "fix" this? Thanks! – Joseph Victor Zammit Mar 26 '12 at 14:32
  • The static file servers are entirely separate from the app servers, and so don't have their own copy of Django. If you want the static file servers to serve these, they have to be uploaded as part of your app. Another option would be to write a little handler that serves them from the app servers, which could find the Django directory using `inspect.getfile(django)` or something like that. (And thanks for the compliment on the book; I'm actively working on a 2nd edition. :) ) – Dan Sanderson Apr 01 '12 at 18:02
  • Fair enough. Accepted this as solution. – Joseph Victor Zammit Apr 02 '12 at 07:39
1

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.

hansaplast
  • 11,007
  • 2
  • 61
  • 75
  • 1
    Will not unmark the other answer as correct, as I can't verify your soultion is correct as I'm currently not using GAE. Still sounds "neater", i.e. +1ed – Joseph Victor Zammit Dec 28 '12 at 13:30
  • This works for me! BTW I use "/static" and "static/" instead of "/static/admin" and "static/admin" in the handlers section of app.yaml. – Golden Thumb Feb 03 '18 at 17:06
1

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.

vault
  • 3,930
  • 1
  • 35
  • 46
0

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.

Harkirat Saluja
  • 7,768
  • 5
  • 47
  • 73
0

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

rroldanh
  • 1
  • 2