简体   繁体   English

为什么在Django模板中找不到CSS文件?

[英]Why the css file can not be found in Django template?

I have created a project in Django. 我已经在Django中创建了一个项目。 Also, I am using django-allauth for sign up and login. 另外,我正在使用django-allauth进行注册和登录。

In order to use my own templates with django-allauth, I have created a html file called signup.html in a folder called account inside a folder called templates which is outside of of all my apps (/templates/account/signup.html) . 为了在django-allauth中使用我自己的模板,我在所有应用程序(/templates/account/signup.html)之外的名为template的文件夹内的account文件夹中创建了一个名为signup.html的html文件。 。 That works. 这样可行。

I tried to use some custom css file inside signup.html: 我试图在signup.html中使用一些自定义的CSS文件:

<link rel="stylesheet" href="/templates/account/signup.css">

It says that the file can not be found. 它说找不到该文件。 Though, it is located in templates/account. 不过,它位于模板/帐户中。

your css file must under STATICFILES_DIRS in settings.py ,set this in settings.py : 您的css文件必须在STATICFILES_DIRS中的STATICFILES_DIRS下,在settings.py中进行settings.py

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '/')
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

and put account/signup.css file to /static/account/signup.css ,you can get it like: 并将account/signup.css文件放入/static/account/signup.css ,您可以像这样获得它:

<link rel="stylesheet" href="/static/account/signup.css">

or 要么

{% load static %}
<link rel="stylesheet" href="{% static 'account/signup.css' %}">

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

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