首页 > 解决方案 > 如何在 Django 中将 CSS 链接到 HTML?

问题描述

这是我的 html 文件:

{% load static %}
<html>
    <head>
        <title>Abhya Tripathi|Developer</title>
        <link rel="stylesheet" href="{% static 'css/blog.css' %}">
    </head>
    <body>
      <nav>
<ul id='navbar'>
  <li><a href="#welcome-section">About</a></li>
  <li><a href="#projects">Work</a></li>
  <li><a href="#contact">Contact</a></li>
</ul>
</nav>

<div id="welcome-section" class="intro">

<h1>Hey i'am Mimic.</h1>
<p>a web developer</p>


</div>
<div id="projects" class="work">
<h2 class="work-header">These are some of my projects..</h2>
<a href="https://codepen.io/FreeCodeCamp/pen/NNvBQW" target="_blank" class="project project-tile">
  <img class="project-pic" src="https://cloud.githubusercontent.com/assets/15967809/17642794/d084d718-6171-11e6-83fa-ede5d0a67ad2.png" alt="project">
  <div class="project-title">Tribute Page</div>
</a>
<a href="https://codepen.io/FreeCodeCamp/pen/ONjoLe" target="_blank" class="project project-tile">
  <img class="project-pic" src="https://cloud.githubusercontent.com/assets/15967809/17642771/7cd6a0c4-6171-11e6-87fb-915f6084d104.png" alt="project">
  <div class="project-title">Random Quote Machine</div>
</a>
<a href="https://codepen.io/FreeCodeCamp/pen/PNKdjo" target="_blank" class="project project-tile">
  <img class="project-pic" src="https://cloud.githubusercontent.com/assets/15967809/17642772/7d02406c-6171-11e6-8c79-40a2933163dc.png" alt="project">
  <div class="project-title">JavaScript Calculator</div>
</a>
<a href="https://codepen.io/FreeCodeCamp/pen/mVEJag" target="_blank" class="project project-tile">
  <img class="project-pic" src="https://cloud.githubusercontent.com/assets/15967809/17642773/7d08cb94-6171-11e6-8c45-22e7cf64683e.png" alt="project">
  <div class="project-title">Map Data Across the Globe</div>
</a>
<a href="https://codepen.io/FreeCodeCamp/pen/wGqEga" target="_blank" class="project project-tile">
  <img class="project-pic" src="https://cloud.githubusercontent.com/assets/15967809/17642774/7d091806-6171-11e6-8d47-ecf2f2833fe2.png" alt="project">
  <div class="project-title">Wikipedia Viewer</div>
</a>
<a href="https://codepen.io/FreeCodeCamp/pen/KzXQgy" target="_blank" class="project project-tile">
  <img class="project-pic" src="https://cloud.githubusercontent.com/assets/15967809/17642775/7d354304-6171-11e6-8b56-66eee4681d88.png" alt="project">
  <div class="project-title">Tic Tac Toe Game</div>
</a>

<a href="https://codepen.io/FreeCodeCamp/" class="show-all" target="_blank">Show all</a>
</div>
<div id="contact" class="contact">
<div class="header">
  <h1>Let's work together...</h1>
  <p>How do you take your coffee?</p>
</div>
<a href="https://facebook.com/freecodecamp" target="_blank" class="contact-details">Facebook</a>
<a id='profile-link' href="https://github.com/freecodecamp" target="_blank" class="contact-details">GitHub</a>
<a href="https://twitter.com/freecodecamp" target="_blank" class="contact-details">Twitter</a>
<a href="mailto:example@example.com" class="contact-details">Send a mail</a>
<a href="tel:555-555-5555" class="contact-details">Call me</a>
</div>
<footer>**This is just a fake portfolio.. All the projects and contact details given are not real..
<p>&copy; Created for <a href="https://www.freecodecamp.com/" target="_blank">FreeCodeCamp</a></p>
</footer>
    </body>
</html>

这是我的 css 文件:

html,
body {
  font-family: 'Open Sans', sans-serif;
  text-align: center;
}

* {
  margin: 0;
  padding: 0;
}

.intro {
  top: 0;
  background: #e0ebe8;
  min-height: 55vh;
  padding-top: 45vh;
  /*   line-height: 100vh; */
  /*   width:100%;
  height:96vh;
border:solid; */
}

.intro p {
  color: green;
}

nav ul {
  text-align: right;
  position: fixed;
  width: 100%;
  background-color: #e0ebe8;
}

nav ul li {
  display: inline-block;
  margin: 55px;
  margin-bottom: 15px;
  margin-top: 25px;
}

nav ul li a {
  color: #008080;
  text-decoration: none;
}

nav ul li a:hover {
  color: green;
}

.work {
  padding-top: 60px;
  margin-top: -60px;
}

.work .work-header {
  margin-top: 50px;
  color: #008080;
  font-weight: normal;
}

.project {
  text-decoration: none;
  color: #008080;
  display: inline-block;
  overflow: hidden;
  border: 2px solid lightblue;
  height: 220px;
  width: 280px;
  margin: 40px 100px;
  /*   background: url("https://res.cloudinary.com/dck40m3wf/image/upload/v1455459914/Portfolio/fancy-floral-pattern-384.jpg") repeat; */
}

.project-pic {
  width: 100%;
  height: 180px;
  border-bottom: 2px solid lightblue;
}

.project-title {
  font-weight: bold;
}

.show-all {
  text-decoration: none;
  display: block;
  width: 200px;
  height: 30px;
  margin: 20px auto 90px;
   background: #e0ebe8;
  color: #008080;
  font-weight: bold;
  font-size: 1em;
  border: 2px solid #90C695;
  border-radius: 4px;
  cursor: pointer;
  padding-top:10px;
}


.show-all:hover {
  background: #E4F1FE;
  color: green;
}

@media only screen and (max-width: 500px) {
  nav ul {
    text-align: center;
  }
  nav ul li {
    margin: 20px;
  }
  .project {
    margin: 40px 0;
  }
}

.contact {
  /*   height:800px; */
  /*   min-height:800px; */
  background: #C8F7C5;
  padding-bottom: 160px;
}

.header {
  padding: 160px 0 110px;
}

.contact-details {
  display: inline-block;
  font-weight: bold;
  margin: 0 35px 45px 35px;
  list-style-type: none;
  border: 2px solid #90C695;
  border-radius: 100%;
  width: 100px;
  height: 100px;
  line-height: 100px;
  background: #e0ebe8;
  color: #008080;
  text-decoration: none;
}

.contact-details:hover {
  background: #E4F1FE;
  color: green;
}

footer {
  padding: 20px;
  background: #22313F;
  color: white;
  line-height: 40px;
}

footer a {
  color: white;
}

@media only screen and (max-width: 367px) {
  /*    #contact{
    min-height:1100px;
  } */
  .contact-details {
    border: 2px solid #90C695;
    border-radius: 0;
    width: 90px;
    height: 20px;
    padding: 10px;
    line-height: normal;
  }
}

这是我的文件夹结构:
在此处输入图像描述

这是我的setting.py:

"""
Django settings for mysite project.

Generated by 'django-admin startproject' using Django 1.11.13.

For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'hzfd@hm@%=n8kgsb$@bw^$i%jl(=sn5d$3ifwx4)uy#jrwu9!9'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['127.0.0.1', '.pythonanywhere.com']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog',

]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'mysite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'mysite.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Kolkata'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

我已将 CSS 文件放在静态文件夹下。我尝试将链接中的源更改为“../blog.css”。
我确信问题不在于设置文件,因为我尝试了一些旧项目的 html 和 CSS 内容,然后它就可以工作了。

标签: htmlcssdjango

解决方案


我相信你的文件结构应该是 blog/static/blog/css/blog.css。你的标签看起来像

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

由于我无法更好地解释它,这里是 django 网站上关于如何找到静态文件的摘录:

“就像模板一样,我们也许可以将静态文件直接放在 polls/static 中(而不是创建另一个 polls 子目录),但这实际上是个坏主意。Django 将选择它找到的第一个静态文件名称匹配,如果您在不同的应用程序中有一个具有相同名称的静态文件,Django 将无法区分它们。我们需要能够将 Django 指向正确的文件,确保这一点的最简单方法是命名空间。也就是说,将这些静态文件放在另一个以应用程序本身命名的目录中。“


推荐阅读