Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
504 views
in Technique[技术] by (71.8m points)

django - 如何在生产中使用Django模板在weasyprint中渲染图像?(How do I get an image to render in weasyprint using a Django Template in production?)

I am trying to get an image to render using Weasyprint with Django in production.

(我正在尝试使用Weasyprint和Django在生产中渲染图像。)

I can get it to work with debug=True on my local machine, but cannot get it to work on my apache server with debug=True or False or with debug=False on my local machine.

(我可以在本地计算机上将其与debug = True一起使用,但不能在本地计算机上以debug = True或False或在debug = False的情况下使其在我的apache服务器上工作。)

It's like it can't render out the url for the image source properly.

(就像它无法正确呈现图像源的url。)

The PDF is returned properly, and if I stick the {% static 'image.png' %} in the template as text it comes back with:

(PDF会正确返回,如果我将{%static'image.png'%}作为文本粘贴在模板中,它将返回:)

/static/img/image.png

What exactly am I missing?

(我到底在想什么?)

My filestructure is as follows:

(我的文件结构如下:)

www 
    |__app_root
    |   |__static
    |   |   |__img
    |   |      |__image.png
    |   |__urls.py
    |   |__views.py
    |__settings_root
        |__settings.py

settings.py

(settings.py)

DEBUG = False
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'app_root/static')

views.py

(views.py)

html_string = template.render(context)
pdf_file = HTML(string=html_string, base_url=request.build_absolute_uri('/')).write_pdf(stylesheets=[CSS(settings.STATIC_ROOT + '/print.css')])
# have also tried request.build_absolute_uri() without the '/'

template.html

(template.html)

{% load static %}
<img src="{% static 'image.png' %}"/>

Apache Configuration - default-ssl.conf & 000-default.conf

(Apache配置-default-ssl.conf和000-default.conf)

Alias /static /var/www/app_root/static
<Directory /var/www/app_root/static>
    Require all granted
</Directory>

Error Message

(错误信息)

"GET /static/img/image.png HTTP/1.1" 404 77
  ask by tpubbsGIS translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...