[英]Django unable to load static files
Completely new to django.对 Django 来说是全新的。
In my settings.py file, I have:在我的 settings.py 文件中,我有:
STATIC_URL = '/static/'
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
My urls.py has我的 urls.py 有
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index)
]
And my views.py has我的 views.py 有
def index(request):
return render(request, 'index.html', {})
Finally, I have the following in my index.html:最后,我的 index.html 中有以下内容:
{% load static %}
<link href="{% static 'styles/custom.css' %}" rel="stylesheet">
The HTML file loads like it showed but the styles don't show up. HTML 文件像显示的那样加载,但样式不显示。 On inspection, it says that the CSS file was not found.
检查时,它说找不到 CSS 文件。
Here is my directory structure:这是我的目录结构:
.
├── README.md
├── app
│ ├── __init__.py
│ ├── db.sqlite3
│ ├── manage.py
│ ├── models.py
│ ├── settings.py
│ ├── urls.py
│ ├── views.py
│ └── wsgi.py
├── db.sqlite3
├── static
│ ├── scripts
│ │ └── custom.js
│ └── styles
│ └── custom.css
└── templates
└── index.html
You have to tell Django where to find the static files.您必须告诉 Django 在哪里可以找到静态文件。 It is done by setting the STATICFILES_DIRS on your settings.py module.
这是通过在settings.py模块上设置STATICFILES_DIRS来完成的。
Usually we do something like this, using the BASE_DIR
:通常我们做这样的事情,使用
BASE_DIR
:
settings.py设置.py
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
It is a little bit confusing in the beginning, because Django has three similar configuration:一开始有点让人迷惑,因为Django有三个类似的配置:
STATIC_URL
STATIC_ROOT
STATICFILES_DIRS
Basically, the STATIC_URL
will be used to generate a URL to serve your static assets.基本上,
STATIC_URL
将用于生成一个 URL 来为您的静态资产提供服务。 It could be a relative URL like you had:它可能是一个像你这样的相对 URL:
STATIC_URL='/static/'
{% static 'styles/custom.css' %} => /static/styles/custom.css
It could be a full URL:它可能是一个完整的 URL:
STATIC_URL='https://static.yourdomain.com/'
{% static 'styles/custom.css' %} => https://static.yourdomain.com/styles/custom.css
Now, the STATICFILES_DIRS
is where Django will find your static files.现在,
STATICFILES_DIRS
是 Django 将找到您的静态文件的地方。 It's usually within the project folder.它通常在项目文件夹中。 BUT!
但是! This is not from where Django will serve your static assets to the client.
这是从哪里Django会为你的静态资产到客户端。 It's like, where is the source of your static files .
就像,您的静态文件的来源在哪里。
That's because Django doesn't really serve this kind of files ( .js , .css , .jpg ).那是因为 Django 并没有真正提供这种文件( .js 、 .css 、 .jpg )。 Well, not by default anyway.
好吧,无论如何都不是默认的。 You could use a third-party app like WhiteNoise .
您可以使用像WhiteNoise这样的第三方应用程序。
And that's also why we have the STATIC_ROOT
, which is to tell Django where to copy those files from the STATICFILES_DIRS
so someone else can serve it for me .这也是我们有
STATIC_ROOT
,它告诉 Django在哪里从STATICFILES_DIRS
复制这些文件,以便其他人可以为我提供服务。 This someone else is usually Apache or NGINX.这个别人通常是 Apache 或 NGINX。
We only use the STATIC_ROOT
for deployment.我们只使用
STATIC_ROOT
进行部署。
When we deploy a Django application to a productions server, we usually run the command python manage.py collectstatic
.当我们将 Django 应用程序部署到生产服务器时,我们通常运行命令
python manage.py collectstatic
。 And that's where Django make a copy of the files from STATICFILES_DIRS
to the STATIC_ROOT
.这就是 Django 将文件从
STATICFILES_DIRS
复制到STATICFILES_DIRS
的STATIC_ROOT
。
But during the development phase you don't really have to care about this much detail, because while DEBUG=True
, Django will make things easier for you and serve the static files.但是在开发阶段,你真的不必关心这么多细节,因为虽然
DEBUG=True
,Django 会让你更轻松,并为静态文件提供服务。
Add these in your settings.py :在settings.py 中添加这些:
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static_in_env')]
VENV_PATH = os.path.dirname(BASE_DIR)
STATIC_ROOT = os.path.join(VENV_PATH, 'static_root')
MEDIA_ROOT = os.path.join(VENV_PATH, 'media_root')
Here is settings.py code:这是 settings.py 代码:
STATIC_URL = "/static/"
STATICFILES_DIR = [
os.path.join(BASE_DIR, "static")]
LOGIN_URL = "account:login"
LOGIN_REDIRECT_URL = "public:index"
LOGOUT_REDIRECT_URL = "public:index"
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'website.apps.accounts',]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.