Django不提供静态文件,也没有风格化任何东西 [英] Django not serving static files and not stylizing anything

查看:73
本文介绍了Django不提供静态文件,也没有风格化任何东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在计算机上以zip文件的形式下载了模板.它有一个主页文件 auth-login.html .如果我自己加载它,那么它可以正确加载,我会看到样式,并且不会收到任何控制台错误.

I downloaded a template in the form of a zip file on my machine. It has a file for a homepage, auth-login.html. If I load this on its own then it loads correctly, I see styling and I don't get any console errors.

但是似乎我无法通过 DEBUG = true 通过 python manage.py runserver 在我的Django项目中加载此模板的CSS和样式.我试图将其放在开发服务器上,但实际上并没有走过第一步.当我尝试进入应用程序的主页时,在浏览器中看到的是不经典的罗马文字.页面上完全没有样式加载.我也没有收到服务器/控制台错误.

But it seems like I can't get this template to load its css and styling in my Django project via python manage.py runserver with DEBUG=true. I'm trying to just get this on a development server and I haven't really been able to get past step 1. When I try to go to my application's home page, I see unstylized times new roman text in my browser. No styling loads on the page at all. I'm not getting server/console errors either.

我的Django项目具有以下结构

My Django project is of the following structure

lpbsproject/ 
   project_root/
     staticFiles/ (STATIC_ROOT, where collectstatic copies to)
     project_app/
       settings.py
       urls.py
       wsgi.py, asgi.py, __init__.py...        
     static/ (STATIC_URL, original location of static files)
       assets/ (this folder is copied/pasted from the template .zip)
         css/, js/, ...
     user_auth/
       migrations
       views.py
       admin.py, models.py, apps.py, test.py ...
     templates/
     manage.py

这是我的html文件的< head> ,其中包含所有< link> < script> 语句.这些目前不会产生错误.

Here's the <head> of my html file with all the <link> and <script> statements. These currently aren't generating errors.

{% load static %}
<!doctype html>
<html lang="en">

    <head>
        <title>Login template from online</title>
        <link rel="shortcut icon" href="{% static 'assets/images/favicon.ico' %}">
        <link href="{% static 'assets/css/bootstrap.min.css' %}" id="bootstrap-style"/>
        <link href="{% static 'assets/css/icons.min.css' %}" type="text/css" />
        <link href="{% static 'assets/css/app.min.css' %}" id="app-style"  type="text/css" />
        <script src="{% static 'assets/libs/jquery/jquery.min.js' %}"></script>
        <script src="{% static 'assets/libs/bootstrap/js/bootstrap.bundle.min.js' %}"></script>
        <script src="{% static 'assets/libs/metismenu/metisMenu.min.js' %}"></script>
        <script src="{% static 'assets/libs/simplebar/simplebar.min.js' %}"></script>
        <script src="{% static 'assets/libs/node-waves/waves.min.js' %}"></script>
        <script src="{% static 'assets/js/app.js' %}"></script>
    </head>

在settings.py中,这是指定BASEDIR和静态文件位置的方式

In settings.py, this is how BASEDIR and the static file location are specified

BASE_DIR = Path(__file__).resolve().parent.parent # points to project_root/ directory
STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticFiles')

project_root/static/assets/中有大约1200个文件,我看到这些文件已从 python manage.py collectstatic 复制到 project_root/staticFiles 中代码>.昨晚我在处理一些问题,其中没有通过collectstatic命令复制任何js文件.

There's ~1200 files in project_root/static/assets/ and I am seeing these get copied into project_root/staticFiles from python manage.py collectstatic. Last night I was dealing with some weridness where none of the js files were getting copied via the collectstatic command.

和urls.py出于好奇...

and urls.py for curiosity sake...

from django.contrib import admin
from django.urls import path
from django.contrib import admin
from django.urls import path, include
from user_auth import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.userLogin, name='loginHome'),
    path('login', views.userLogin, name='login'),
    path('logout', views.userLogout, name='logout'),
    path('registration', views.userRegistration, name='registration'),
    path('dashboard', views.dashboard, name='dashboard'),
]

因此,如果 python manage.py collectstatic 正常工作,为什么我仍然看不到任何样式?我不太确定这是怎么回事.仅仅为该项目使用一个简单的/static/文件夹感觉太困难了.

So if python manage.py collectstatic is working... why am I still not able to see any styling at all? I'm not really sure what's going wrong here. It's felt way too difficult to just get a simple /static/ folder working for this project.

使用 python manage.py runserver 的终端甚至没有显示对大多数这些CSS或JS文件的请求.我只是看到

The terminal using python manage.py runserver isn't even showing requests for most of these css or js files. I just see

[24/Nov/2020 12:48:00] "GET / HTTP/1.1" 200 7194
[24/Nov/2020 13:25:39] "GET /static/assets/libs/bootstrap/js/bootstrap.bundle.mi
n.js.map HTTP/1.1" 404 1883
[24/Nov/2020 13:25:39] "GET /static/assets/libs/metismenu/metisMenu.min.js.map HTTP/1.1" 404 1853
[24/Nov/2020 13:25:39] "GET /static/assets/libs/node-waves/waves.min.js.map HTTP /1.1" 404 1844

推荐答案

当我在django论坛上发布有关此问题的信息时,我就能得到答案.

I was able to get an answer when I posted about this on django forums.

这里没有人发现html错误.我不一致地使用 type = text/css rel ='stylesheet'.同样,< link> 标签可以以> 结尾,而不是/> .

No one here caught the html errors. I was inconsistently using type=text/css and rel='stylesheet'. Also <link> tags can end with >, not />.

这篇关于Django不提供静态文件,也没有风格化任何东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆