Bootstrap不会设置为Django应用程序

use*_*674 11 css python django bootstrap-4

Bootstrap没有设置为我的Django应用程序.在谷歌控制台,无法加载资源:服务器响应状态为404(未找到)bootflat.min.css错误发生.我在settings.py中写道

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEBUG =True

STATIC_URL = '/static/'

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

STATICFILES_DIRS = [os.path.join(BASE_DIR,'bootflat.github.io'), ]
Run Code Online (Sandbox Code Playgroud)

在PythonServer_nginx.conf中

server {
    # the port your site will be served on
    listen      8000;
    # the domain name it will serve for
    server_name MyServerIPAdress; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    location /static {
        alias /home/ubuntu/PythonServer/PythonServer/accounts/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /home/ubuntu/PythonServer/uwsgi_params; # the uwsgi_params file you installed
    }
}
Run Code Online (Sandbox Code Playgroud)

我不知道为什么Bootstrap没有被设置为我的Django应用程序.我运行命令python manage.py collectstatic但没有发生错误.我该怎么解决这个问题?我该怎么写呢?

小智 9

我假设该项目在本地工作正常,只有当您使用nginx时才会出现此问题.如果是这种情况,您在nginx配置中提到的位置/静态路径可能是错误的,请尝试从服务器获取静态文件夹的绝对路径并查看.


pkq*_*xdd 5

我没有办法以明显的理由来测试我的答案,但是你的STATICFILES_DIRSSTATIC_ROOT不匹配.如果你改变你STATICFILES_DIRS[os.path.join(BASE_DIR,"/static/"),'https://bootflat.github.io'],它可能会奏效.我建议这样做,因为对我来说,os.path.join使用公共URL的本地路径没有意义.


Pra*_*rma 5

试试这个,在settings.py中:

STATIC_DIR = os.path.join(BASE_DIR,"static")
Run Code Online (Sandbox Code Playgroud)

最后,添加以下行:

STATIC_URL = '/static/'
STATICFILES_DIRS = [STATIC_DIR,]
Run Code Online (Sandbox Code Playgroud)

现在,将所有引导程序文件放在项目文件夹下的"静态"文件夹中.

在HTML代码中,您必须使用以下命令加载静态文件:

{% load staticfiles %}
Run Code Online (Sandbox Code Playgroud)

现在,您可以使用以下方法使用静态文件:

<link rel="stylesheet" href="{% static "assets/css/bootstrap.min.css" %}">
Run Code Online (Sandbox Code Playgroud)

Ps我的'bootstrap.min.css'出现在'static'文件夹下,然后是'assets'然后是'css'文件夹.

它对我来说很完美.