简体   繁体   English

我如何将 css 文件加载到 Django 中的 html

[英]How do i load css file into html in Django

I'm trying to create a Django app and i have made all the static settings like我正在尝试创建一个 Django 应用程序,并且我已经进行了所有 static 设置,例如

Settings.py设置.py

STATICFILES_DIRS = [
    os.path.join(BASE_DIR,'CricketTeamManagement/static')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

MEDIA_URL = '/media/'

URLS.py URLS.py

from django.conf import  settings
from django.conf.urls.static import static
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('Management.urls'))
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

I have also created a static folder in Main project folder and ran python manage.py collectstatic it created a new static folder outside and it has admin folder in it and sub folders like js,css,font,images and collectstatic collects the images and stores in this folder. I have also created a static folder in Main project folder and ran python manage.py collectstatic it created a new static folder outside and it has admin folder in it and sub folders like js,css,font,images and collectstatic collects the images and stores在这个文件夹中。 I see static files are clearly working.我看到 static 文件显然可以正常工作。

All the models.py imagefields are uploaded to media folders and even that works.所有的models.py图像字段都上传到媒体文件夹,甚至可以正常工作。

The Problem comes here问题来了

My css files are not taking its styles and not altering my html elements.我的 css 文件没有采用它的 styles 并且没有改变我的 html 元素。 If I have the below code in html file the styling works and if i separate it to a css folder and create a css file by removing style tag it is not working.如果我在 html 文件中有以下代码,则样式有效,如果我将其分离到 css 文件夹并通过删除样式标签创建 css 文件,则它不起作用。

 <style> body { background-color: powderblue; } h1 { color: blue; } p { color: red; } </style>

when i saw the css file Not Found error it got fixed with当我看到 css 文件未找到错误时,它得到了修复to make html check the css file.制作 html 检查 css 文件。

Unsure what is the issue.不确定是什么问题。 Thanks in Advance.提前致谢。

Update Project Format Link Tag in HTML更新HTML 中的项目格式链接标签

you have to link your css files this way in templates, also collectstatic command is not required during production stage only您必须在模板中以这种方式链接您的 css 文件,并且仅在生产阶段不需要 collectstatic 命令

{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}"/>

The href in your index.html right now refers to yourwebsite/stylesheet.css.您的 index.html 中的 href 现在指的是您的网站/样式表.css。 Django doesn't know where that URL is as you defined your static URL as "/static/". Django doesn't know where that URL is as you defined your static URL as "/static/". Static located in the STATIC_DIRS will be loaded under the URL yourwebsite/static/stylesheet.css.位于 STATIC_DIRS 中的 Static 将被加载到 URL yourwebsite/static/stylesheet.css 下。 This is why you are getting the 404 on the CSS file.这就是您在 CSS 文件中获得 404 的原因。

Change your index.html to use Django built-in template tags to make it easier.更改您的 index.html 以使用 Django 内置模板标签,使其更容易。

{% load static %}
Some code here.....
<link rel="stylesheet" type='text/css' href="{% static 'css/stylesheet.css' %}">

This will automatically generate the right url for your static file which should solve the problem.这将自动为您的 static 文件生成正确的 url 文件,这应该可以解决问题。

You don't need to run collect static in development as django's server will take care of the static files.您不需要在开发中运行 collect static,因为 django 的服务器会处理 static 文件。 During production will will need to run collect static and point your reverse proxy or the like towards the static files.在生产期间将需要运行收集 static 并将您的反向代理等指向 static 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 html 文件中加载 css 文件已经在 Z2B9AFB89A36ACC15ADZ5B159 中扩展了另一个 html 文件(base.html) - how to load css file in html file already extends another html file (base.html) in django Django CMS如何将CSS文件添加到管理员 - Django CMS how do I add a CSS file to the admin 如何在 css 文件中包含背景图片以供 django 使用? - How do I include a Background picture in a css file for django usage? 如何将html文件加载/显示到QTextBrowser小部件中? - How do I load/display an html file into my QTextBrowser widget? 如何使用 HTML/CSS/JS 前端和 Django 和 Flask 后端创建 Django Web 应用程序? - How do I create a Django web application with an HTML/CSS/JS frontend and a Django and Flask backend? 如何在 Django 中使用 CSS + JavaScript 文件渲染 HTML - How To Render An HTML With Css + JavaScript File In Django 我无法使用 django 链接 html 文件中的 CSS 文件 - I can't link CSS file in the html file on with django 如何在开发中为Django提供CSS? - How do I serve CSS to Django in development? 如何使用 wxPython 将带有外部链接的 HTML 文件加载到 CSS 文件? - How to load HTML file with external link to CSS file with wxPython? 如何在Django中将base.css与base.html文件链接? - How should I link base.css with base.html file in Django?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM