【问题标题】:Static Files would not load in Django (Pycharm)静态文件不会在 Django (Pycharm) 中加载
【发布时间】:2019-01-21 14:16:16
【问题描述】:

我目前正在使用 Django 框架制作一个 Web 应用程序,并且最近才开始。我查看了 Django 文档,还查看了 StackOverflow 上的许多教程和其他答案,但它们似乎都不起作用。当我第一次将链接放在 HTML 页面上时,我包括了{% static 'my_app/css/cssFile'%},还包括了{% load staticfiles %}。我也尝试将其包含在我的设置文件中:STATIC_ROOT = posixpath.join(*(BASE_DIR.split(os.path.sep) + ['static'])) 但这些都不起作用。每次我尝试运行服务器时,控制台总是说服务器找不到资源并以 404 的形式退出。

这是我的设置文件:

    """
Django settings for WebApp project.

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

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

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

import os
import posixpath

# 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/2.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '6-_^kvfdcg&@+_gdf1ub*ood*$fm4vs1m-aw_uw#(2tliu9(d0'

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

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'WebApp',
    '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 = 'WebApp.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')]
        ,
        '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 = 'WebApp.wsgi.application'


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

STATIC_URL = '/static/'

STATIC_ROOT = posixpath.join(*(BASE_DIR.split(os.path.sep) + ['static']))

我用于测试的非常简单的 HTML 文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ title }} - My Django Application</title>
    {% load staticfiles %}
    <link rel="stylesheet" type="text/css" href="{% static 'WebApp/css/layout.css' %}">
</head>
<body>
    <div class="navbar clearfix">
        <div class="container">
            <div class="logo"></div>
            <div class="menu-item">
                <ul class="item-list">
                    <a href="#" class="items">Home</a>
                    <a href="#" class="items">About</a>
                    <a href="#" class="items">Question</a>
                </ul>
            </div>
        </div>
    </div>

    <div class="content">
        <div class="container-content">
            {% block content %}{% endblock %}
        </div>
    </div>

    {% block scripts %}{% endblock %}
</body>
</html>

还有我制作的 CSS 文件:

div.navbar {
    width: 100%;
    color: #FFF;
    height: 10px;
    background-color: #000000;
}

.clearfix {
    content: "";
    clear: both;
    display: table;
}

.container {
    background-color: #000;
    width: 100%;
    height: 100%;
}

我的目录设置如下:

任何帮助将不胜感激。

【问题讨论】:

  • 打印 STATIC_ROOT 看看到底有什么

标签: python html css django pycharm


【解决方案1】:

现在使用这个检查这个即时通讯并且可以工作。

设置

STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,"media")

我就是这样打电话的

<link href="{% static 'app/vendor/bootstrap/css/bootstrap.min.css'%}" rel="stylesheet">

在网址中

if settings.DEBUG:
    from django.conf.urls.static import static
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

【讨论】:

  • 媒体是您放置图片的地方吗?
  • src="{% static 'app/img/book1.png'%}"
【解决方案2】:

更改下一个模板标签:

{% load staticfiles %}

为:

{% load static %}

【讨论】:

    猜你喜欢
    • 2017-11-08
    • 1970-01-01
    • 2012-04-30
    • 2018-11-16
    • 2020-09-10
    • 2017-10-08
    • 2019-02-23
    • 2017-04-08
    • 2017-11-08
    相关资源
    最近更新 更多