CSS 未加载到 Django 项目中? [英] CSS not loading in Django project?

查看:23
本文介绍了CSS 未加载到 Django 项目中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Django 的新手,但我的任务是为项目做前端工作.我一直在研究如何准确加载 css 文件,而我发现的方法不起作用.这是 .html 文件布局:

{% 加载静态 %}<头><title class="title">NHSEE 主页 </title><!--<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"><link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">--><link rel="stylesheet" href="{% static 'css/homepage.css' %}"><身体><div class="容器"><h1>NHSEE</h1><a class="btn btn-1" href="/judges">Judges</a><br><a href="{% url 'students' %}">Students</a><br><a class="btn btn-1" href={% url 'scoring-sheet' %}>计分表</a><br><a href="/students">Students</a><br><a href="/projects">项目</a>

这是文件的布局(不确定我是否将导致问题的文件放在哪里)任何帮助是极大的赞赏.非常令人沮丧,因为我之前专业地完成了前端工作并且无法显示一个愚蠢的 css 文件.

settings.py 文件

解决方案

所以,首先你需要在你的settings.py中有STATICFILES_DIRS:

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'assets')]

然后继续将您所有的资产(css、js、字体等)移动到manage.pyassets 的目录中代码>(不在你的应用中)

举个例子,假设你有 assets/css/home_page.css;您应该在模板中使用 {% static 'css/home_page.css' %} 来访问它(省略 assets 目录).

这是基本思想,您可以根据需要命名这些目录.

I'm new to Django but have been tasked with doing the front-end work for the project. I've been researching how exactly to load the css files and the methods I've found just aren't working. Here is the .html file layout:

{% load static %}
<html>
<head>
    <title class="title"> NHSEE Homepage </title>
    <!--<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css"> -->
    <link rel="stylesheet" href="{% static 'css/homepage.css' %}">
</head>
<body>
<div class="container">
    <h1> NHSEE </h1>
    <a class="btn btn-1" href="/judges">Judges</a>
    <br>
    <a href="{% url 'students' %}">Students</a>
    <br>
    <a class="btn btn-1" href={% url 'scoring-sheet' %}>Scoring Sheet</a>
    <br>
    <a href="/students">Students</a>
    <br>
    <a href="/projects">Projects</a>
</div>

Here is the layout of the files (not sure if its where I'm putting the files that causing the issue) Any help is greatly appreciated. Its very frustrating as I've done frontend work professionally before and can't get a stupid css file to show up.

EDIT: The settings.py file

解决方案

So, first you need to have STATICFILES_DIRS in your settings.py:

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'assets')]

Then go ahead and move all your assets (css, js, fonts, ...) to a directory called assets beside manage.py (not inside your apps)

as an example, let's say you have assets/css/home_page.css; you should use {% static 'css/home_page.css' %} in your templates to access that (omitting the assets directory).

That was the basic idea, you can name those directory as you need.

这篇关于CSS 未加载到 Django 项目中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆