我正在使用 reportlab pdfgen 创建 PDF。在 PDF 中有一个由 drawImage
创建的图像。为此,我需要图像的 URL 或 View 中图像的路径。我设法构建了 URL,但如何获取图像的本地路径?
我如何获取 URL:
prefix = 'https://' if request.is_secure() else 'http://'
image_url = prefix + request.get_host() + STATIC_URL + "images/logo_80.png"
最佳答案
# Older Django <3.0 (also deprecated in 2.0):
from django.contrib.staticfiles.templatetags.staticfiles import static
# Django 3.0+
from django.templatetags.static import static
url = static('x.jpg')
url 现在包含 '/static/x.jpg'
,假定静态路径为 '/static/'
关于Django 在 View 中获取静态文件 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11721818/