为什么更改我的“STATIC_URL”设置会改变我的Django应用程序是否有效?

时间:2022-06-01 08:45:53

In settings.py, if I specify STATIC_URL = 'http://68.164.125.221/', then clients can visit my web page. However, no CSS and other static files load.

在settings.py中,如果我指定STATIC_URL ='http://68.164.125.221/',则客户端可以访问我的网页。但是,没有加载CSS和其他静态文件。

On the other hand, if I specify STATIC_URL = '/', then visiting the home page of my application produces this error.

另一方面,如果我指定STATIC_URL ='/',那么访问我的应用程序的主页会产生此错误。

Page not found (404)
Request Method: GET
Request URL:    http://68.164.125.221/
Directory indexes are not allowed here.
You're seeing this error because you have DEBUG = True in your Django settings file.

Interestingly, my static files now load (http://68.164.125.221/main.css loads for instance) despite the home page of my app displaying this error.

有趣的是,我的静态文件现在加载(例如http://68.164.125.221/main.css加载)尽管我的应用程序的主页显示此错误。

Why does changing my "STATIC_URL" setting toggle whether my home page loads or my static files load? I am using the staticfiles app.

为什么更改我的“STATIC_URL”设置会切换我的主页加载还是我的静态文件加载?我正在使用staticfiles应用程序。

1 个解决方案

#1


1  

You probably want STATIC_URL = '/static/'[1]; when you set it to / Django is trying to serve up /blah/ as a static file rather than pushing it through to your Django app.

你可能想要STATIC_URL ='/ static /'[1];当你把它设置为/ Django试图提供/ blah /作为一个静态文件而不是推送到你的Django应用程序。

The actual error you're getting, Directory indexes are not allowed here., refers to the fact that the static file folders aren't listed out - you must access a specific file for it to be served up.

你得到的实际错误,这里不允许使用目录索引。指的是没有列出静态文件夹的事实 - 你必须访问一个特定的文件才能提供它。

[1] Or /media/, or /files/, or whatever... just not '/'.

[1]或/ media /,或/ files /,或其他......只是不是'/'。

#1


1  

You probably want STATIC_URL = '/static/'[1]; when you set it to / Django is trying to serve up /blah/ as a static file rather than pushing it through to your Django app.

你可能想要STATIC_URL ='/ static /'[1];当你把它设置为/ Django试图提供/ blah /作为一个静态文件而不是推送到你的Django应用程序。

The actual error you're getting, Directory indexes are not allowed here., refers to the fact that the static file folders aren't listed out - you must access a specific file for it to be served up.

你得到的实际错误,这里不允许使用目录索引。指的是没有列出静态文件夹的事实 - 你必须访问一个特定的文件才能提供它。

[1] Or /media/, or /files/, or whatever... just not '/'.

[1]或/ media /,或/ files /,或其他......只是不是'/'。