简体   繁体   中英

Django static files 404 not found nginx

I am developing a Django application and I want to put it online on DigitalOcean so I can show it to a remote future user. I am quite new to django and python and I have never in my life touched the deployment of web applications, so I'm a bit lost in all that.

I used the one-click install of a django droplet in digitalocean, which uses nginx and gunicorn. I was able to make the app load in the browser using the ip address (159.203.58.210), but the problem is that no static files can be loaded.

I googled that for some days and nothing could help me. I'm guessing it has something to do with either permissions not allowed or that I did not write the static files correcly in my templates and *.py files.

I'm running my application with the command:

python manage.py runserver localhost:9000

I'm using sqlite3 as db

My complete code is here: https://github.com/gbastien1/carte-interactive

On the server, my app is at /home/django/international (where international is my site name, the app is called carte_interactive)

My staticfiles settings in settings.py:

STATIC_URL = '/static/'
STATIC_ROOT= '/home/django/international/carte_interactive/static'

my staticfiles settings in /etc/nginx/sites-enabled/django.conf (same for media)

location /static {
    alias /home/django/international/carte_interactive/static;
}

In my templates, I use { % load staticfiles %} And I call my static files like so:

{% static 'carte_interactive/css/style.css' %}

And in my .py files, I sometimes call those files like this, which I don't know if it's right:

app_name='carte_interactive'
json_data_url = static('carte_interactive/json/data.json')
json_data_file = open(app_name + json_data_url, 'w') <-- here

It was the only solution that worked on development to access my static files in views.py, but it might be a problem in production with collectstatic and all, I don't know.

I am using django 1.9.4 on server and 1.9.1 on development it seems.

Do you have any idea why my staticfiles are 404 not found on the server but everything works fine on development?

Could there be a permission issue with the folder /home/django/... for the browser to reach my static files?

Is there a better way to call my staticfiles in views.py?

Thanks in advance!

edit: The console error I get for all my template static files:

http://159.203.58.210/static/carte_interactive/css/style.css Failed to load resource: the server responded with a status of 404 (Not Found)

I assume that you don't use nginx to serve static assets in development? Runserver can serve static files, but very much slower than nginx, which becomes a problem once you have more than a single web site visitor at a time. You can remove the nginx alias for static and reload nginx to let runserver serve the files, to confirm whether it's a problem in the nginx config. Håken Lid

I removed nginx and made the Django server load the static files, and now I can show it to my future users. This answered my question, though it did not solve the problem itself! Thanks anyway !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM