Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

python - Django not serving static files and not stylizing anything

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.

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.

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

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>

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')

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.

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'),
]

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...