我正在尝试在我的网络应用程序中显示图像,但由于某种原因,它没有显示并给出 404 错误。我认为在 urls.py 中我正确添加了静态,即使将其更改为 STATIC_ROOT 仍然没有意义。同时IDE没有给出任何错误只是静态不起作用。
设置.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