简体   繁体   English

无法在Django中加载.css和图像文件

[英]Can't load .css and image files in django

I can't load images and css files in django, although everything seems to be fine in settings.py and in the home.html file itself... what can be a problem here? 我无法在django中加载图像和css文件,尽管在settings.py和home.html文件本身中似乎一切都很好……这里可能有什么问题?

In the static folder there are template , css and image folders. 在静态文件夹中,有templatecssimage文件夹。

HTML: HTML:

{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'css/main.css' %}">
<title>Main Page</title>

settings.py: settings.py:

    # Static files (CSS, JavaScript, Images)
MEDIA_ROOT = os.path.join(BASE_DIR, "/media")
#MEDIA_URL = ''
#STATIC_ROOT = os.path.join(BASE_DIR, "static")

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "/static")
]
STATIC_URL = "/static/"

Unfortunately, it looks like this: 不幸的是,它看起来像这样: 俄语网页,没有CSS格式和图像损坏

django.contrib.staticfiles is not included in your INSTALLED_APPS. django.contrib.staticfiles不包含在您的INSTALLED_APPS中。

Your settings.py should look like this (in INSTALLED APPS) 您的settings.py应该看起来像这样(在已安装的应用中)

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.sites',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

without that static files won't work even if everything else is setup properly because to django it's not installed even though it is (Technically). 如果没有django,则即使配置正确,静态文件也无法正常工作,因为django即使技术上也没有安装,也不会安装。

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

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