为什么我的index.html不会连接到我的index.css文件?

时间:2017-06-16 00:59:35

标签: python html css django

我的index.html由于某种原因无法连接到我的index.css。我不明白为什么。也许这是文件或其他东西的路径。我为这样一个noob问题道歉。我的index.css文件只有一小段代码,只是为了测试并查看它是否有效。

enter image description here

这是我的index.html文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>The Page</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    {% load static %}
    <link rel="stylesheet" href="{% static 'index.css' %}">
</head>

<body>
    <div class="container">
        <form method="POST">
        {% csrf_token %}

            <div class="form-group">
                <label for="firstName">First Name:</label>
                <input class="form-control" id="firstName" placeholder="Enter first name" name="firstName">
            </div>

            <div class="form-group">
                <label for="">Last Name:</label>
                <input class="form-control" id="lastName" placeholder="Enter last name" name="lastName">
            </div>

            <div class="checkbox">
                <label><input type="checkbox" name="remember">Remember me</label></div></br>
                <button type="submit" class="btn btn-default">Submit</button>
            </div>

            </div>
        </form>
    </body>
</html>

这是我的index.css文件:

html {
    background: chocolate;
}

这是我的settings.py文件:

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 = '+-9mn+q&u@lw2_=s7&=zin5d7oxbt#v@9jg%2+a7=#noqd_jyf'

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

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'music.apps.MusicConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]



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 = 'tyran.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 = 'tyran.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 = 'UTC'

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/'

4 个答案:

答案 0 :(得分:3)

你应该在Django中以正确的方式load the static files,更好地分割静态文件和模板。以下是基于您的案例的一种解决方案:

首先,在music文件夹下,创建static文件夹,放置静态文件index.css

之后,在index.html

中更改加载css文件
{% load static%}
<link rel="stylesheet" href="{% static 'index.css' %}">

然后,它将加载静态文件。

<强>更新

确保index.css位于路径中:tyran/music/static/index.css

答案 1 :(得分:0)

只是给它一条艰难的道路:

<link rel="stylesheet" href="~/music/templates/music/index.css">

答案 2 :(得分:0)

Django正在寻找一个名为“index.css”的文件,该文件相对于项目根目录,而不是模板目录。请参阅此文档(https://docs.djangoproject.com/en/1.11/howto/static-files/#serving-static-files-during-development),了解如何设置STATIC_URL目录以告知Django在哪里查找静态资产。

答案 3 :(得分:0)

代码中没有任何问题。 我复制了你的代码并在我的机器上运行。它工作正常。

enter image description here

您的代码没有问题。连接也太正确了。尝试在其他IDE中或手动执行此操作。