Django静态图像显示然后不显示,视图共享静态徽标图像所在的基础Html文件



我的djangoprojectdir/static/media/文件夹中的徽标显示在我的主页上,但没有显示在其他视图中。我不知道为什么它没有出现在其他页面上——它共享相同的base.html文件作为导航栏和标题。目前托管的网站正在制作中的数字海洋,nginx,gunicorn。我只是想在没有数据库的小项目的生产调试中提供静态文件。我已经收集了静电。该图像仅适用于我的IndexView。与我的对象相关的其他静态图像文件似乎渲染良好。我一直在竭尽全力想办法弄清楚这一点。

有问题的base.html的图像代码是:

<a class="navbar-brand" href="{% url 'maps:index' %}"><img src="static/media/Dirt logo3.png" hieght="5" class="img-fluid" alt="Responsive image"> </a>

我尝试过使用{{MEDIA_URL}}/Dirt-logo3.png和{{STATIC_URL}}进行映射,但没有成功,如果可以的话,硬编码也可以。

来自base.html

#bootstrap stuff here#
{% load static from staticfiles %}
<a class="navbar-brand" href="{% url 'maps:index' %}"><img src="static/media/Dirt logo3.png" hieght="5" class="img-fluid" alt="Responsive image"> </a>

{% block body %}
{% endblock %}

来自设置.py

STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'mysite', 'maps', 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'mysite', 'maps', 'static', 'media')
MEDIA_URL = 'static/media/'
MEDIA_DIR = os.path.join(BASE_DIR, "")

Views.py

# works on IndexView but not any other view
class IndexView(generic.ListView):
template_name = 'maps/index.html'
paginate_by = 20
def get_queryset(self):
return Location.objects.all()
context_object_name = 'location'
class NeeddirtView(generic.ListView):
template_name = 'maps/needdirt.html'
paginate_by = 20
def get_queryset(self):
return Location.objects.all()
context_object_name = 'location'

来自maps/urls.py

from django.urls import path
from . import views
from django.conf import settings
from django.conf.urls.static import static

app_name = 'maps'
urlpatterns = [
# /maps/
path('', views.IndexView.as_view(), name='index'),
# /maps/71/
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('register/', views.UserFormView.as_view(), name='register'),
path('login/', views.LoginView.as_view(), name='login'),
path('logout/', views.LogoutView.as_view(), name='logout'),
# /maps/companies/
path('companies/', views.CompView.as_view(), name='comp'),
# /maps/equipment/
path('equipment/', views.EquipView.as_view(), name='equip'),
#maps/
path('equipment/<int:pk>/', views.EquipdetView.as_view(), name='equipdet'),
path('location/add/', views.LocationCreate.as_view(), name='location-add'),
path('location/<int:pk>/', views.LocationCreate.as_view(), name='location-update'),
path('location/<int:pk>/delete/', views.LocationDelete.as_view(), name='location-delete'),
path('equipment/add/', views.EquipCreate.as_view(), name='equipment-add'),
path('equipment/<int:pk>/', views.EquipCreate.as_view(), name='equipment-update'),
path('equipment/<int:pk>/delete/', views.EquipDelete.as_view(), name='equipment-delete'),
path('quarry/', views.QuarryView.as_view(), name='quarry'),
path('quarry/<int:pk>/', views.QuarrydetView.as_view(), name='quarrydet'),
path('quarry/add/', views.QuarryCreate.as_view(), name='quarry-add'),
path('quarry/<int:pk>/', views.QuarryCreate.as_view(), name='quarry-update'),
path('quarry/<int:pk>/delete/', views.QuarryDelete.as_view(), name='quarry-delete'),
path('search/', views.search, name='search'),
path('test/', views.TestView.as_view(), name='test'),
path('needdirt/', views.NeeddirtView.as_view(), name='needdirt'),
path('havedirt/', views.HavedirtView.as_view(), name='havedirt'),
path('about/', views.AboutView.as_view(), name='about'),
]

来自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

urlpatterns = [
path('', include('maps.urls')),
path('admin/', admin.site.urls),
path('maps/', include('maps.urls')),
]

urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

非常感谢您的帮助。

问题很可能是由于您的相对路径造成的。尝试:

<img src="/static/media/Dirt logo3.png"... />

注意前导斜杠,使其相对于根。

我不喜欢文件名中的空格,所以我也会更改它。

最新更新