静态文件图像未找到 Django
Static file images Not found Django
我正在制作一个网站,但没有加载静态文件,
当我尝试使用 localhost:8000/static/image.png 访问静态文件时,它显示如下:
Page Not found (404)'image.png' could not be found
并且索引页正在显示找不到图像时显示的图像
个文件:
VoicesOnTheSpectrum
VoicesOnTheSpectrum
__init__.py
asgi.py
settings.py
urls.py
wsgi.py
vots
migrations
static
image.png
logo.png
style.css
votstop.png
templates
vots
base.html
index.html
__init__.py
admin.py
apps.py
models.py
tests.py
urls.py
views.py
manage.py
settings.py:
from pathlib import Path
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, "vots", "templates")
SECRET_KEY = 'secret'
DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = [
'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 = 'VoicesOnTheSpectrum.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_DIR,],
'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 = 'VoicesOnTheSpectrum.wsgi.application'
# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'
STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static')]
# STATIC_ROOT = os.path.join(BASE_DIR, "static")
索引:
{% extends "vots/base.html" %}
{% load static %}
{% block content %}
<div role="img">
<img src="{% static 'votstop.png' %}">
</div>
{% endblock %}
基地:
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}{% endblock %}Voices On The Spectrum</title>
<link rel="stylesheet" type="text/css" href="{% static 'style.css' %}">
</head>
<body>
<div class="navbar">
</div>
<div class="body">
{% block content %}
{% endblock %}
</div>
<div class="footer">
<p>Copyright © {% now "Y" %} Voices on the Spectrum - All Rights Reserved.</p>
<div class="insta">
<a href="https://www.instagram.com/voicesonthespectrum/"><img src="{% static 'image.png' %}" alt="Instagram"></a>
</div>
</div>
</body>
</html>
您需要将视图添加到 urlpatterns
以提供静态(和媒体)文件:
# VoicesOnTheSpectrum/urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# … other urls patterns …
]
urlpatterns += <b>static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)</b>
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
我的回答
只需要将我的应用程序添加到已安装的应用程序
INSTALLED_APPS = [
'vots',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
我正在制作一个网站,但没有加载静态文件, 当我尝试使用 localhost:8000/static/image.png 访问静态文件时,它显示如下: Page Not found (404)'image.png' could not be found 并且索引页正在显示找不到图像时显示的图像
个文件:
VoicesOnTheSpectrum
VoicesOnTheSpectrum
__init__.py
asgi.py
settings.py
urls.py
wsgi.py
vots
migrations
static
image.png
logo.png
style.css
votstop.png
templates
vots
base.html
index.html
__init__.py
admin.py
apps.py
models.py
tests.py
urls.py
views.py
manage.py
settings.py:
from pathlib import Path
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, "vots", "templates")
SECRET_KEY = 'secret'
DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = [
'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 = 'VoicesOnTheSpectrum.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_DIR,],
'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 = 'VoicesOnTheSpectrum.wsgi.application'
# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'
STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static')]
# STATIC_ROOT = os.path.join(BASE_DIR, "static")
索引:
{% extends "vots/base.html" %}
{% load static %}
{% block content %}
<div role="img">
<img src="{% static 'votstop.png' %}">
</div>
{% endblock %}
基地:
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}{% endblock %}Voices On The Spectrum</title>
<link rel="stylesheet" type="text/css" href="{% static 'style.css' %}">
</head>
<body>
<div class="navbar">
</div>
<div class="body">
{% block content %}
{% endblock %}
</div>
<div class="footer">
<p>Copyright © {% now "Y" %} Voices on the Spectrum - All Rights Reserved.</p>
<div class="insta">
<a href="https://www.instagram.com/voicesonthespectrum/"><img src="{% static 'image.png' %}" alt="Instagram"></a>
</div>
</div>
</body>
</html>
您需要将视图添加到 urlpatterns
以提供静态(和媒体)文件:
# VoicesOnTheSpectrum/urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# … other urls patterns …
]
urlpatterns += <b>static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)</b>
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
我的回答
只需要将我的应用程序添加到已安装的应用程序
INSTALLED_APPS = [
'vots',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]