[英]Image is not showing up on my website (django)
我是 django 的新手。 我想上传一张图片,但它没有出现在网站上。 相反,它显示了一个损坏的图像图标。 我尝试使用加载 static 块,但它仍然不起作用。
我的 html 文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Website</title>
</head>
<body>
<h1>Hello</h1>
{% load static %}
<img src="{% static 'webdev/static/webdev/images/images.jpg' %}">
</body>
urls.py 文件:
from django.contrib import admin
from django.urls import path
from homepage import views
urlpatterns = [
path('', views.home_page, name='home'),
path('admin/', admin.site.urls),
]
视图.py 文件:
from django.shortcuts import render
from django.http import HttpResponse
def home_page(request, *args, **kwargs):
return render(request, 'home.html', {})
在开发期间提供 static 文件的文档解释了如何设置 static 文件。 您需要将提供 static 文件的视图添加到urlpatterns
:
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path
from homepage import views
urlpatterns = [
path('', views.home_page, name='home'),
path('admin/', admin.site.urls),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
当DEBUG
设置为True
时,这将提供 static 文件。 在生产的情况下( DEBUG = False
),Django不提供 static 文件。 然后您应该配置网络服务器 (nginx/apache/...) 以提供 static 和媒体文件,或者使用 CDN。
此外,路径是:
<img src="{% static 'webdev/images/images.jpg' %}">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.