Django 在视图中获取静态文件 URL [英] Django get the static files URL in view

查看:48
本文介绍了Django 在视图中获取静态文件 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 reportlab pdfgen 创建 PDF.在 PDF 中有一个由 drawImage 创建的图像.为此,我需要图像的 URL 或视图中图像的路径.我设法构建了 URL,但如何获取图像的本地路径?

I'm using reportlab pdfgen to create a PDF. In the PDF there is an image created by drawImage. For this I either need the URL to an image or the path to an image in the view. I managed to build the URL but how would I get the local path to the image?

我如何获取网址:

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/'

url now contains '/static/x.jpg', assuming a static path of '/static/'

这篇关于Django 在视图中获取静态文件 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆