【发布时间】:2020-11-28 06:21:09
【问题描述】:
我的 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 %}
来自settings.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'
来自地图/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)
非常感谢任何帮助。
【问题讨论】:
标签: django django-views django-templates