模板不显示图像
我用
<img src="/static/logo_coorg.png" width="60" height="40">
以及诸如以下的设置:[已更改]
STATIC_URL = 'static/'
MEDIA_URL = 'media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
PROJECT_ROOT = os.path.normpath(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
网址为:
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('erp.urls')),
]
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
这样我的 django 网站就没有任何问题了。但另一个新的 django 网站确实是一个绝望的网站,出现了这个错误。自过去 24 小时以来我一直在尝试解决此问题。 寻求帮助。
在模板文件中,首先使用以下命令加载静态文件:
{% load static %}
然后使用静态模板标签:
<img src="{% static '/logo_coorg.png' width="60" height="40" %}"
当您的
DEBUG
模式为 False
时,您不需要为 MEDIA_URL
或 STATIC_URL
添加 URL 模式,因为 Django 不负责提供静态或媒体文件。你可以这样写:
from django.conf import settings
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
您应该处理 Apache Web 服务器配置文件中的媒体和静态 URL 路径,以提供静态和媒体文件。例如:
Alias /media/ full_path_of_your_media_diractory
<Directory full_path_of_your_media_diractory>
Require all granted
</Directory>
Alias /static/ full_path_of_your_static_diractory
<Directory full_path_of_your_static_diractory>
Require all granted
</Directory>
检查网址中的斜杠(我可能是错的)。
如果仍然不起作用,请将您的目录结构和网络服务器配置添加到问题中。