在Django中返回图片给前端通常有两种方式:
<img src="{{ image_url }}" alt="Image"> 在视图函数中需要将图片的URL传递给模板进行渲染,示例代码如下:
from django.shortcuts import render def image_view(request): image_url = "/path/to/image.jpg" return render(request, "image.html", {"image_url": image_url}) from django.http import HttpResponse import os def image_view(request): image_path = "/path/to/image.jpg" with open(image_path, "rb") as f: image_data = f.read() return HttpResponse(image_data, content_type="image/jpeg") 以上是两种常见的方式,具体选择哪种方式取决于项目的需求和设计。