简体   繁体   English

静态文件不会在Django(Pycharm)中加载

[英]Static Files would not load in Django (Pycharm)

I am currently making a Web App using the Django framework and have just started recently. 我目前正在使用Django框架制作Web应用程序,并且最近才开始使用。 I looked through the Django Documentation and also looked through many tutorials and other answers on StackOverflow but none of them seem to work. 我浏览了Django文档,还浏览了StackOverflow上的许多教程和其他答案,但似乎都没有用。 When I first placed the link on the HTML page I included the {% static 'my_app/css/cssFile'%} and also included {% load staticfiles %} . 当我第一次将链接放置在HTML页面上时,我包含了{% static 'my_app/css/cssFile'%} ,还包含了{% load staticfiles %} I have also tried including this into my settings file: STATIC_ROOT = posixpath.join(*(BASE_DIR.split(os.path.sep) + ['static'])) but none of these work. 我也尝试将其包含到我的设置文件中: STATIC_ROOT = posixpath.join(*(BASE_DIR.split(os.path.sep) + ['static']))但这些都不起作用。 Every time I try to run the server, the console keeps saying that the server could not find the resource and that it exited out as a 404. 每当我尝试运行服务器时,控制台都会不断说服务器找不到资源,并且它以404退出。

This is my settings file: 这是我的设置文件:

    """
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']))

My very simple HTML file that I am using for testing: 我用于测试的非常简单的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>

And the CSS file that I made: 还有我制作的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%;
}

I have set up my directories as follows: 我将目录设置如下: 我如何建立Django专案的树

Any help would be greatly appreciated. 任何帮助将不胜感激。

Check this im using this now and works. 检查此即时消息,现在就可以使用。

Settings 设置

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

and i call in this way 我这样打电话

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

in urls 在网址中

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

Change the next template tag: 更改下一个模板标签:

{% load staticfiles %}

for: 对于:

{% load static %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM