python - css 未加载到我的 Django 登录和管理页面中

标签 python css django

我是django的新手,我开始通过官方教程学习django。

我在 virtualenv 旁边使用 django,但我有一个问题 登录页面和管理页面,因为

他们不加载 css 和显示登录 没有任何样式的页面和管理页面

"""
Django settings for mysite project.

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

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

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

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

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.8/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'g8%o@ackd!hzekoho4rn7r7-t_m!sk$*nwi-4j556t=!ln3(@+'

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

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

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.8/ref/settings/#databases

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


# Internationalization
# https://docs.djangoproject.com/en/1.8/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.8/howto/static-files/

STATIC_URL = '/static/'

最佳答案

首先,创建名为“static”的文件夹,然后您需要将所有 css/js 文件复制到元素中的静态文件夹(或您创建静态文件夹的任何地方)。然后在你的 settings.py 声明静态文件目录路径

STATIC_URL = '/static/'
STATICFILES_DIRS = ('assets', BASE_DIR +'/static/',)

在您的 html 文件中,添加以下行 -
{% load staticfiles %}
在标题部分的顶部或文件的顶部。
<head>部分您可以使用以下代码链接 css/js 文件

<link rel="stylesheet" href="{% static 'assets/css/mystyle.css'%}">
</link>
<script src="{% static 'assets/js/jquery.js'%}"></script>

关于python - css 未加载到我的 Django 登录和管理页面中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30071257/

相关文章:

javascript - 滚动条问题 x2

html - Chrome 和 Firefox 中的 'div' 高度不同

html - 在 Chrome PDF 查看器中加载时 PDF 链接卡住

Django 休息框架。 raise_exception=真

python - RubyPython 无法在 OS X Lion 上导入 nltk

python - 键盘中断tensorflow运行并在此时保存

javascript - 在页面加载时将焦点设置在滚动框上

python - Django Queryset 使用 .extra 查询多对多关系

python - PyDev 交互式控制台

python - 接收列表 (myList) 和值 (n) 并检查该值是否在该列表中的函数