3

I am attempting to deploy a Django 1.6 application on OpenShift using the Python 3.3 cartridge, but I have run into problems with static files. I have had partial success with the OpenShift IRC channel, tutorials/templates (for example), and previous StackExchange questions (for example), but nothing has completely resolved the problem.

When I request the static content by URL (e.g. 'mydomain.com/static/stylesheet.css' or 'mydomain.com/static/icons/cog.svg') I can see them perfectly fine. When static files are used as SVG data for icons, they show up fine. Only when linking to a stylesheet have I run into problems. I use the following to include CSS in my template:

<link type="text/css" rel="stylesheet" href={% static "stylesheet.css" %}/>

I have loaded the static files tag set with {% load staticfiles %}. Instead of seeing the stylesheet at /static/stylesheet.css, Django (I assume that it is Django, not Apache) looks for it at /static/stylesheet.css/ (note the trailing slash). This causes the request to fail with a 404 status code. The same thing occurs when I use other file extensions (I have tried .txt, .css, and .svg) or link to a file contained in a subdirectory of static. It is only in this circumstance that an extra trailing slash is appended.

It is my understanding that Django appends a trailing slash to a URL in the event that the URL does not match any of the patterns defined in urls.py. Is it possible on OpenShift to configure Apache so that it directly handles all requests to URLs of the form /static/*? I have an .htaccess file in the wsgi directory with the commands

Rewrite Engine On
Rewrite Rule ^application/static/(.+)$ /static/$1 [L]

but this does not solve the problem. I have also tried using a rewrite rule for just the stylesheet as well as a few things with Alias but have had no luck there, either.

Should Django be getting the requests for these static files at all? I have confirmed that DEBUG is being set to False in my settings.py file, and make no mention of django.views.static.serve in my urls.py file. Here are the relevant parts of settings.py:

STATIC_URL = '/static/'
if 'OPENSHIFT_REPO_DIR' in os.environ:
    STATIC_ROOT = os.path.join(os.environ.get('OPENSHIFT_REPO_DIR'),
                               'wsgi', 'static')
else:
    STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')

I do not set values for STATICFILES_DIRS or STATICFILES_FINDERS because at present I'm only dealing with static files found at STATIC_ROOT. The OpenShift project looks like

~/app-root/runtime/repo/wsgi/
    .htaccess
    application
    openshift/
        settings.py
        manage.py
        #And so on.
    static/
        stylesheet.css
        icons/
            cog.svg
            #More icons here.

This is my first time trying to deploy and I am stuck on this stumbling block. Does anyone know what I am doing wrong?

Community
  • 1
  • 1
Ben Whitney
  • 204
  • 1
  • 9

1 Answers1

5

Instead of href={% static "stylesheet.css" %}, try href="{% static 'stylesheet.css' %}"

Mohammad Siavashi
  • 1,192
  • 2
  • 17
  • 48
rookie
  • 2,783
  • 5
  • 29
  • 43
  • 1
    I think you mean `href="{% static 'stylesheet.css' %}"`, as per [URL names](https://docs.djangoproject.com/en/1.9/intro/tutorial03/#namespacing-url-names). Nevertheless, definitely +1 - I managed to completely miss the extra set of double-quotes previously. – John C Apr 29 '16 at 19:56