我有一个简单的Django应用程序,它是在Nginx托pipe的。 我正在使用weasyprint来生成PDF报告。 weasyprint需要一个base_url属性来访问static文件。
虽然下面的django代码在本地机器上工作正常(在dev服务器下),但在Nginx后面发布502错误网关错误。
View.py
html = render_to_string('admin/enquiry/quoterequest/generate.html',{'enquiry': enquiry}) response = HttpResponse(content_type='application/pdf') response['Content-disposition'] = 'filename="Enquiry_{}.pdf'.format(enquiry.reference) weasyprint.HTML(string=html,base_url=request.build_absolute_uri()).write_pdf(response,stylesheets=[ weasyprint.CSS(settings.STATICFILES_Dirs[0] + '/css/print.css')])
上面的代码工作正常(没有打印图像),如果我删除了base_url属性。 将不胜感激您的input – 如何设置Nginx或从Django回收base_url
Nginxconfiguration
# configuration of the server server { listen 80; server_name 192.168.33.10; # Vagrant IP root /home/www/my_project; charset utf-8; client_max_body_size 75M; # max upload size location /media { alias /home/www/my_project/assets/uploads; } location /static { alias /home/www/my_project/assets/static; } location / { proxy_pass http://localhost:8001; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }
print.css
@page { size: letter; margin: 1.5in .25in 1.9in .5in; @top-center { content: url("/uploads/header_footer/header.jpg"); height: 100%; width: 100%; } @bottom-center { background-image: url("/uploads/header_footer/footer.jpg"); background-repeat: no-repeat; background-position: center; content: "Page " counter(page); height: 100%; width: 100%; vertical-align: bottom;; } }
如何在Windows中创build一个python文件可执行文件
使用与Windows和Python 2.6.1的猎豹模板系统(名称映射问题)
子域与文件夹/目录
重新configurationApache以从新的PHP源代码和旧的Django站点的特定子网站提供网站根目录
无法从Windows cmd安装Django。 尽pipePATH应该设置,“pip install django”显示错误
upstream prematurely closed connection while reading response header from upstream,client: 192.168.33.1,server: 192.168.33.10,request: "GET /enquiry/admin/enquiry/quoterequest/view/1/ HTTP/1.1",upstream: "http://127.0.0.1:8001/enquiry/admin/enquiry/quoterequest/view/1/",host: "192.168.33.10",referrer: "http://192.168.33.10/admin/enquiry/quoterequest/"
在虚拟环境中获取Django以通过Upstart运行
在Ubuntu 11.10上通过Nginx使用Django + uWsgi
django request.POST包含<无法parsing>
如何访问django项目之外的目录文件?
回答我自己 – 但这是一种解决方法“。 对于这样的IP 192.168.33.10 ,并为它的基地址http://192.168.33.10/media/' base_url parameter for仍然有问题 – 即使手动输入基地址也没有办法。
这仍然不起作用,并返回与502坏网关
weasyprint.HTML(string=html,base_url='http://192.168.33.10/media/').write_pdf(response)
所以我决定改变template 。 所以无论我在哪里定义了一个URL,我都将它们改为…
<img src="http://{{ request.Meta.HTTP_HOST }}{{ MEDIA_URL }}{{ myapp.mymodel.my_image }}">
并在View.py添加context_instance以获得MEDIA_URL 。 希望有人会为weasyprint的base_url问题提出一个答案。
html = render_to_string('admin/enquiry/quoterequest/generate.html',{'enquiry': enquiry},context_instance=RequestContext(request)) response = HttpResponse(content_type='application/pdf') response['Content-disposition'] = 'filename="Enquiry_{}.pdf'.format(enquiry.reference) weasyprint.HTML(string=html,stylesheets=[ weasyprint.CSS(settings.STATICFILES_Dirs[0] + '/css/print.css')])
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。