Django -加载css的问题



我最近开始学习Django,并且在代码中实现css时有很多问题。我发现很多人和我有同样的问题,但我仍然找不到答案。

目前当我运行打开网站它给我这个https://i.stack.imgur.com/cL1I3.jpg

我不确定这是否因为设置,实际的css或html中的东西。

我的html模板

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>homepage</title>
<link rel="stylesheet" type="test/css" href="{% static'css/home.css' %}"/>
</head>

settings.py

DEBUG = True
STATIC_ROOT = ''
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
ALLOWED_HOSTS = ['127.0.0.1', 'localhost']
STATIC_URL = '/static/'

views.py

from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from leatherbiscuit.views import index
urlpatterns = [
path('admin/', admin.site.urls),
path('', index),
]
if settings.DEBUG:
urlpatterns+=static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)   

urls . py

from django.shortcuts import render
from django.http import HttpResponse
from django.views.generic import TemplateView

def index(request):
return render(request, 'index.html')
def home_view(request):
return HttpResponse(request, 'index.html') 

您需要在模板标签的名称和它的参数之间添加一个空格,所以它应该是:

↓ a space between static and 'css/home.css'
{% static 'css/home.css' %}

Django模板引擎的解析器有一些特殊之处。例如,标签不应该跨越多行,并且像在Python的方法调用中一样,应该首先列出位置参数,然后列出命名参数。