Serving Static files - Django-nonrel in Appengine

506 Views Asked by At

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.

1

There are 1 best solutions below

5
On

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.