【问题标题】:Django - issue with loading cssDjango - 加载css的问题
【发布时间】:2021-11-07 15:33:56
【问题描述】:

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

目前,当我打开网站时,它会给我https://imgur.com/a/0N23s7b

我不确定这是因为设置、实际的 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') 

【问题讨论】:

    标签: python html css django


    【解决方案1】:

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

             &downarrow; a space between static and 'css/home.css'
    {% static 'css/home.css' %}

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

    【讨论】:

      猜你喜欢
      • 2020-03-22
      • 2019-12-21
      • 2012-07-19
      • 1970-01-01
      • 1970-01-01
      • 2020-12-06
      • 1970-01-01
      • 2023-01-08
      • 2021-05-31
      相关资源
      最近更新 更多