简体   繁体   English

在django项目中加载静态文件时出错

[英]Error in loading static files in django project

I've created django project and in settings.py configured all the necessary assets 我创建了django项目,并在settings.py中配置了所有必要的资产

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
STATICFILES_DIRS = [
    "~/<project_name>/static",
]

I have all of my static files based in the folder static in the project directory. 我有我所有的基于文件夹的静态文件static的项目目录。 Inside this folder I have css, js, images folders accordingly. 在这个文件夹里面我有css,js,相应的图像文件夹。 In my base template (that is inherited by others) called base.html I have template tag {% load staticfiles %} . 在我的基本模板(由其他人继承)中,名为base.html我有模板标签{% load staticfiles %} I can't figure out why the page still loads without any static attached, because everything seems to be made properly. 我无法弄清楚为什么页面仍然加载没有任何静态附加,因为一切似乎都正确。

Change to this (more in STATICFILES_DIRS setting ): 更改为此(更多在STATICFILES_DIRS设置中 ):

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]

And inside your templates fetch static files like this (Django 1.10): 在模板中获取这样的静态文件(Django 1.10):

{% load static %}

<link rel="stylesheet" href="{% static 'css/css-file.css' %}">

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

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