【问题标题】:python built in server not loading csspython内置服务器不加载css
【发布时间】:2014-05-15 09:05:26
【问题描述】:

当我运行服务器时,django 管理员缺少 css。Web 控制台显示未加载样式表,因为它的 MIME 类型“application/x-css”不是“text/css”。 这是我的 settings.py 文件

mysite 项目的 Django 设置。

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
    # ('Your Name', 'your_email@example.com'),
)

MANAGERS = ADMINS

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3', 
        'NAME': 'C:/djangorevision/mysite/sqlite3.db',                       database 
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                   
        'PORT': '',                     
    }
}

ALLOWED_HOSTS = []


TIME_ZONE = 'America/Chicago'


LANGUAGE_CODE = 'en-us'

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True


USE_TZ = True

MEDIA_ROOT = ''

MEDIA_URL = ''


STATIC_ROOT = 'C:/djangorevision/mysite/static'

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (

)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
 #    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

# Make this unique, and don't share it with anybody.
SECRET_KEY = '*4#u_xz-+7cp-x-)w(o*sr1&1$^fa409_d@7!&z%_(93u=@s=o'

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'mysite.urls'

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'mysite.wsgi.application'

TEMPLATE_DIRS = (
  )

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
     'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
    'polls'

)

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,`enter code here`
        },
    }
}

【问题讨论】:

    标签: python django python-2.7 static-files


    【解决方案1】:

    答案有点晚,但为了完整起见,需要将其放在这里。

    我在一个开发系统上也遇到了这个奇怪的错误(我没有在同一个项目的其他开发系统上遇到这个错误)。可能和这个系统上安装了Dreamweaver(可以打开css文件)有关?

    无论如何,为了解决这个问题,我在 settings.py 中添加了以下内容:

    导入 mimetypes mimetypes.add_type("text/css", ".css", True)

    这将在您的 mimetypes 列表中添加(如果已经存在则替换)正确的 css mimetype。

    如果您不想将其放入您的 settings.py 中,请将其添加到您的设置包的 __init__.py 文件中。

    【讨论】:

    • 我包含了上述内容,但仍然出现错误。我的系统中安装了 Dreamweaver
    • 你把它放在哪里了?你确定它被执行了吗?尝试在mimetypes.add_type 之后添加打印语句以确保它运行。
    • 我将它包含在我的settings.py中,我还添加了打印语句并执行了。
    • 嗯...它适用于我的情况,我真的不知道是什么问题:|
    • 上面的答案解决了这个问题,但是很高兴你在 Dreamweaver 上介绍了这个问题并得到了帮助
    【解决方案2】:

    我刚刚遇到了同样的问题。看来您对 Dreamweaver 的看法是正确的。将上述字符串添加到 settings.py 对我没有帮助。

    我已经在 regedit 上运行搜索 .css 并更改了最可疑的注册表项/记录

    1. 可能与将 .css 文件视为 application/x-css 类型有关
    2. 可能与将 .css 文件扩展名与打开应用程序相关联,尤其是 Dreamweaver。

    我花了两次迭代,我不能确定哪一个是正确的,因为它可能需要一些时间来更新一些缓存,等等。

    虽然我在两个位置进行了更改:

    1. HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.css
    2. HKEY_CLASSES_ROOT\.css

    我重命名(通过添加前导“--”以便在必要时可以恢复)与所有应用程序(包括 Dreamweaver、记事本等)关联的 .css 文件相关的密钥/记录并声明 .css 文件作为 application/x-css 类型。

    之后问题得到解决,Django CSS 样式按预期正确应用。

    【讨论】:

    • 您能否清楚地说明您重命名的内容。我运行了 regedit,但无法清楚地知道我要在这些层次结构中重命名什么
    • HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.css 中找到以下记录Name: Content Type; Type: REG_SZ; Value: application/x-css,其中NameTypeValue 是regedit 窗口右侧的列标题。点击Content Type。调用上下文菜单(右键单击)并选择“重命名”。在Content Type 之前键入--,这样新名称将显示--Content Type。对 Value 列中带有 text/css 的另一条记录重复相同的步骤。转至HKEY_CLASSES_ROOT\.css。您将找到相同的两条记录。重复相同的步骤。
    • 它工作得很好......我也认为 Dreamweaver 有帮助,因为在另一台没有安装 Dreamweaver 的机器上它工作正常
    猜你喜欢
    • 1970-01-01
    • 2017-12-23
    • 2018-06-23
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-19
    相关资源
    最近更新 更多