0

I've successfully got a basic Django-nonrel app up and running on Appengine. The templates are getting rendered properly, but the static content returns a 404 response.

There is no problem with the static content in the dev server launched using `python manage.py runserver'.

These are the relevant lines in static.py:

STATIC_URL = '/static/'
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',  # Refers to PROJECT_DIR/static
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',  # Appname/static
)

STATICFILES_DIRS = (os.path.join(PROJECT_DIR, 'static'),)

In the relevant template:

{% extends "base.html" %}
{% load staticfiles %}
{% block title %}Adding Objects{% endblock %}

{% block content %}
<p>Placeholder for Objects</p>
    <img src="{% static "test_pattern.gif" %}">
    <img src="{% static "sample_overlay.gif" %}">
{% endblock %}

With this, static files in myproject/static directory and myproject/myapp/static directory are being served successfully in the dev server (python manage.py runserver).

This is my app.yaml:

application: appname
version: 1
runtime: python27
api_version: 1
threadsafe: yes

builtins:
- remote_api: on

inbound_services:
- warmup

libraries:
- name: django
  version: latest

handlers:
- url: /_ah/queue/deferred
  script: djangoappengine.deferred.handler.application
  login: admin

- url: /_ah/stats/.*
  script: djangoappengine.appstats.application

- url: /media/admin
  static_dir: django/contrib/admin/media
  expiration: '0'

- url: /.*
  script: djangoappengine.main.application

Any clue how to fix this? I don't want the Appengine web server to handle static files, I want to route everything through Django (at least for now). Hence a solution like this isn't really acceptable in my case.

EDIT: I can easily get around this with this in my app.yaml and serving all static files from projectdir/static.

- url: /static
  static_dir: static

But this solution seems dirty, I'd like to leave it all to Django.

user1265125
  • 2,608
  • 8
  • 42
  • 65

1 Answers1

0

Your adding the /static mapping in app.yaml is the correct method. It is not "dirty".

Also, you are adding the django library in app.yaml. That is not correct. Django-nonrel uses its own branch of Django, which you should import as a directory with your app. Adding the django call to libraries in app.yaml means you are importing 2 versions of Django, which can cause strange errors. Delete the Django library call in app.yaml, and import the Django version that is included with nonrel.

GAEfan
  • 11,244
  • 2
  • 17
  • 33
  • Can you explain why it's the correct method, in short? The way I see it, Appengine is simply delegating all responsibility to Django via `- url: /.*`. So Django should be able to serve static files on its own. Or is it a matter of how Appengine handles deployment? – user1265125 Jun 03 '14 at 20:03
  • Also, on your other point, I simply used the sample django-testapp provided by django-nonrel. You do make a point though, if I remove the django import in `app.yaml`, my app still works. – user1265125 Jun 03 '14 at 20:10
  • It is clean. app.yaml runs first, so immediately serves the proper response. The alternative would be to have some remapping/redirect in urls.py. That requires more processing time, looking up the redirect. Plus, it also would serve a 301 to the user: – GAEfan Jun 03 '14 at 20:11
  • Also, depending on which version of Django you import, you may find your admin static media urls to be incorrect. In some past version, the path was changed to: django/contrib/admin/static/admin/. And, I recommend using the latest version nonrel supplies, which I believe is 1.5.5. – GAEfan Jun 03 '14 at 20:17
  • Have google serve static files is much faster and these files can be served without starting up an instance to run django. There is almost no use case to use django static file serving over appengines native static support. – Tim Hoffman Jun 04 '14 at 00:27