【问题标题】:static file css not loading in django静态文件 css 未在 django 中加载
【发布时间】:2022-01-20 07:59:06
【问题描述】:

我已经尝试了我所知道的一切来解决这个问题,除了 css 之外,所有静态文件都可以正常工作

views.py

from django.http.response import HttpResponse
from django.shortcuts import render

def index(response):
    return render(response , "main/index.html")



html头

{% extends 'main/base.html' %}
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href= "{%static "main/css/index.css" %}">

    <title> Home Page   </title>
</head>

请注意,如果这会影响任何内容,我会在基本文件中使用引导程序

settings.py


STATIC_URL = '/static/'

STATIC_ROOT = "/Users/aryankaushik/Desktop/visual studio code /django/assets"


STATICFILES_DIRS = [
    BASE_DIR / "static",
]



项目目录

├── admin.py
├── apps.py
├── migrations
│   ├── __init__.py
│   └── __pycache__
│       └── __init__.cpython-39.pyc
├── models.py
├── static
│   └── main
│       ├── css
│       │   ├── base.css
│       │   └── index.css
│       ├── img
│       │   ├── electronics.jpeg
│       │   ├── furniture.jpeg
│       │   └── nature.jpeg
│       └── js
│           └── index.js
├── templates
│   └── main
│       ├── base.html
│       ├── contact.html
│       └── index.html
├── tests.py
├── urls.py
└── views.py

我确定文件夹路径正确,因为图像渲染正确..

提前谢谢你

【问题讨论】:

    标签: python html css django django-static


    【解决方案1】:

    尝试以隐身模式运行网站。

    【讨论】:

    • 已经试过也清除缓存
    【解决方案2】:

    在setting.py中配置这个

    STATIC_URL = '/static/'
    
    STATICFILES_DIRS = [
        BASE_DIR / "static",
    
    ]
    

    如果这不起作用,请清除您的浏览器历史记录静态文件,我认为会受到影响 ‍

    【讨论】:

    • 我已清除缓存我也在使用 django 调试工具栏,它显示静态图像但不显示 css。设置和你的一样
    • 请显示您的项目目录
    • 嘿补充说我不认为问题出在结构上
    • 好吧试试这个使用 base.html 中的静态负载
    • @AryanKaushik 嘿,你还不能加载静态文件吗
    【解决方案3】:
    Try it.
    
    settings.py
    
    STATIC_URL = '/static/'
    STATIC_ROOT = BASE_DIR / 'static'
    STATICFILES_DIRS = [
        BASE_DIR / 'project/static'
    ]
    
    MEDIA_ROOT = BASE_DIR / 'media'
    MEDIA_URL = '/media/'
    
    views.py
    
    def index(request):
        dict = {'test':'test2'}
        return render(request , "main/index.html", context=dict)
    
    index.html
    
    {% extends 'base.html' %}
    
    {% load static %}
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href= "{% static 'index.css' %}">
    
        <title>Index.html</title>
    </head>
    
    
    urls.py
    
    urlpatterns = [
        path('',views.index,name='index'),
    ]
    

    【讨论】:

      【解决方案4】:

      在您的 settings.py 中,您可以将它们动态放置在您的任何项目中

      STATIC_URL = "/static/"
      STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
      
      MEDIA_URL = "/media/"
      MEDIA_ROOT = os.path.join(BASE_DIR, "media")
      
      STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
      

      你在 html 上的版本:错误

      <link rel="stylesheet" href= "{%static "main/css/index.css" %}">
      

      真实版本:

      <link rel="stylesheet" href= "{%static 'main/css/index.css' %}">
      

      不要使用类似的引号,它不起作用

      【讨论】:

      • 我尝试了真正的版本它不起作用我什至尝试在 head 标签中编写 css 它仍然不起作用。它仅在我使用内联 css 时才有效
      【解决方案5】:
      Bro try adding these:
      
      Setting.py:
      
      BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
      STA_DIR = os.path.join(BASE_DIR,'static')
      STATIC_URL = '/static/'
      
      STATICFILES_DIRS = [
          STA_DIR,
      ]
      
      
      Urls.py:
      
      urlpatterns = [
          path('',views.index3,name='index'),
      ]
      
      View.py:
      
      def index3(request):
          my_dict ={'insert_me':'hello i m views.py and now i m under template/firstone'} 
          return render(request,'firstone/index.html',context=my_dict) 
      
      Index.html:
      
          <title>Document</title>
          <link rel="stylesheet" href="{% static "css/style.css" %}">
      </head>
      <body>
          <h1>Hello there Index.HTML here</h1>
          <!-- {{insert_me}} -->
          <img src="{% static "images/kami.jpeg" %}" alt="oh no">
      

      【讨论】:

      • 这不是图像工作正常 css 即使在 head 标签中也不能工作,只能在内联 css 中工作
      猜你喜欢
      • 2020-04-28
      • 2020-08-31
      • 2017-05-22
      • 2020-05-12
      • 1970-01-01
      • 2021-11-07
      • 2019-08-31
      • 2014-11-12
      相关资源
      最近更新 更多