1

I have a Flask application deployed on Apache, and the JavaScript files in the static folder cannot be found

enter image description here

I don't understand what is wrong, here are the files:

Apache conf:

<VirtualHost *:80>
    ServerName japanesepractice.local
    ServerAlias www.japanesepractice.local

    WSGIDaemonProcess japanesepractice user=leonardo group=leonardo threads=5
    WSGIScriptAlias / /var/www/japanesepractice.local/japanese.wsgi
    <Directory /var/www/japanesepractice.local>
        WSGIProcessGroup japanesepractice
        WSGIApplicationGroup %{GLOBAL}
        Order allow,deny
        Allow from all
    </Directory>

    Alias /static/ /var/www/japanesepractice.local/static
    <Directory /var/www/japanesepractice.local/static>
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

app.py:

# -*- coding: utf-8 -*-
from flask import Flask
from flask import render_template
from flask import request

from service import kanjiservice

app = Flask(__name__)


@app.route('/')
def homepage():
    return render_template('index.html')

wsgi file:

#!/usr/bin/python2

activate_this = '/var/www/japanesepractice.local/venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

import sys
sys.path.insert(0, '/var/www/japanesepractice.local')

from app import app as application

template:

<script src="{{ url_for('static', filename='js/responsivevoice.js') }}"></script>
<script src="{{ url_for('static', filename='js/speaking.js') }}"></script>

Could someone help?

3 Answers 3

3

You should only use an Alias for your /static/ routes if you don't want Apache to use your WSGIScriptAlias to handle matching requests. This is good for performance, since requests for static files don't need to engage the WSGI application (except creating the URL), but it may be related to your problems.

You can troubleshoot by removing:

Alias /static/ /var/www/japanesepractice.local/static
<Directory /var/www/japanesepractice.local/static>
    Order allow,deny
    Allow from all
</Directory>

If removing this works, try re-adding it with balanced trailing slashes (/static/ as /static to match the missing slash /var/www/japanesepractice.local/static.

Sign up to request clarification or add additional context in comments.

1 Comment

This is partially correct. I've added my comments below. Your answer was a lifesaver for me.
0
/static 

not

/static/

The issue is that all the apache/debian and ubuntu forums online are incorrect. Its a major oversight. Logan Bertram above points to the issue as part of his answer.

With this and conda containers using the online tutorials in youtube I was able to expose something that was on localhost:5000 on a public server.

Comments

-1

Try adding type="text/javascript"

like below:

`<script type="text/javascript" src="{{  url_for('static', filename='js/responsivevoice.js') }}"> . 
</script>` 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.