简体   繁体   English

Django静态文件404找不到nginx

[英]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. 我正在开发一个Django应用程序,我想将它放在DigitalOcean上,这样我就可以将它显示给远程的未来用户。 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. 我是django和python的新手,我从来没有触及过web应用程序的部署,所以我有点迷失方向。

I used the one-click install of a django droplet in digitalocean, which uses nginx and gunicorn. 我在digitalocean中使用了django droplet的一键安装,它使用了nginx和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. 我能够使用ip地址(159.203.58.210)在浏览器中加载应用程序,但问题是无法加载任何静态文件。

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. 我猜它与任何不允许的权限或我没有在我的模板和* .py文件中正确编写静态文件有关。

I'm running my application with the command: 我正在使用命令运行我的应用程序:

python manage.py runserver localhost:9000

I'm using sqlite3 as db 我使用sqlite3作为db

My complete code is here: https://github.com/gbastien1/carte-interactive 我的完整代码在这里: 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) 在服务器上,我的应用程序位于/ home / django / international(其中international是我的站点名称,该应用程序名为carte_interactive)

My staticfiles settings in settings.py: 我在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) /etc/nginx/sites-enabled/django.conf中的我的静态文件设置(对于媒体也是如此)

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

In my templates, I use { % load staticfiles %} And I call my static files like so: 在我的模板中,我使用{ % load staticfiles %}并且我调用我的静态文件:

{% 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: 在我的.py文件中,我有时会调用这样的文件,我不知道它是否正确:

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. 它是唯一能够在views.py中访问我的静态文件的开发解决方案,但是在使用collectstatic的生产中它可能是一个问题,我不知道。

I am using django 1.9.4 on server and 1.9.1 on development it seems. 我在服务器上使用django 1.9.4,在开发上使用1.9.1。

Do you have any idea why my staticfiles are 404 not found on the server but everything works fine on development? 你知道为什么我的静态文件在服务器上找不到404但是一切正常吗?

Could there be a permission issue with the folder /home/django/... for the browser to reach my static files? 文件夹/ home / django / ...是否存在权限问题,以便浏览器访问我的静态文件?

Is there a better way to call my staticfiles in views.py? 有没有更好的方法在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) http://159.203.58.210/static/carte_interactive/css/style.css无法加载资源:服务器响应状态为404(未找到)

I assume that you don't use nginx to serve static assets in development? 我假设您不使用nginx在开发中提供静态资产? 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. Runserver可以提供静态文件,但比nginx慢得多,一旦你有多个网站访问者,这就成了一个问题。 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. 您可以删除静态的nginx别名并重新加载nginx,让runserver提供文件,以确认它是否是nginx配置中的问题。 Håken Lid HåkenLid

I removed nginx and made the Django server load the static files, and now I can show it to my future users. 我删除了nginx并使Django服务器加载了静态文件,现在我可以将它显示给我未来的用户。 This answered my question, though it did not solve the problem itself! 这回答了我的问题,虽然它本身没有解决问题! Thanks anyway ! 不管怎么说,还是要谢谢你 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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