Django 3.1.7 中未加载静态图像
Static images are not loading in django 3.1.7
我试图在我的网络应用程序中显示图像,但由于某种原因,它没有显示并出现 404 错误。
我认为在 urls.py 中我正确地添加了静态,即使将其更改为 STATIC_ROOT 仍然没有意义。同时 IDE 没有给出任何错误,只是静态不起作用。
settings.py
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'blog.apps.BlogConfig',
'jobs.apps.JobsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'portfolio',
]
# Skipped Database and some other parts.
# STATICFILES_DIRS = [
# os.path.join(BASE_DIR, 'static')
# ] That part is giving error in new django 3.1.7 so I removed.
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
***urls.py***
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
import jobs.views
urlpatterns = [
path('admin/', admin.site.urls),
path('', jobs.views.home, name='home'),
path('blog/', include('blog.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
**home.html**
<img src="{% static 'images/img1.JPG' %}" alt="Couldn't load" class="img-fluid" height=150 width=150>
#this is specific part of the html
Django 3.1.7 使用以下语法:
像这样在项目中构建路径:BASE_DIR / 'subdir'
.
代替subdir,你应该写static
我试图在我的网络应用程序中显示图像,但由于某种原因,它没有显示并出现 404 错误。 我认为在 urls.py 中我正确地添加了静态,即使将其更改为 STATIC_ROOT 仍然没有意义。同时 IDE 没有给出任何错误,只是静态不起作用。
settings.py
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'blog.apps.BlogConfig',
'jobs.apps.JobsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'portfolio',
]
# Skipped Database and some other parts.
# STATICFILES_DIRS = [
# os.path.join(BASE_DIR, 'static')
# ] That part is giving error in new django 3.1.7 so I removed.
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
***urls.py***
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
import jobs.views
urlpatterns = [
path('admin/', admin.site.urls),
path('', jobs.views.home, name='home'),
path('blog/', include('blog.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
**home.html**
<img src="{% static 'images/img1.JPG' %}" alt="Couldn't load" class="img-fluid" height=150 width=150>
#this is specific part of the html
Django 3.1.7 使用以下语法:
像这样在项目中构建路径:BASE_DIR / 'subdir'
.
代替subdir,你应该写static