将reportlab图插入到django模板中 [英] Insert reportlab chart into django template

查看:249
本文介绍了将reportlab图插入到django模板中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在django模板中呈现一个reportlab barchart。



这个 django文档给出了在django视图中实现reportlab-chart生成的优秀说明,而这个

(可以使用-to-drawing-object-to-create-pdf-and-return-via-httpresponse)
$ / code code
$ / code code code $ c $ pre>

如何将binaryStuff插入django模板?例如:

 #views.py 
def barChart(request,word_id):
从图表导入BarChartDrawing

word = get_object_or_404(Word,pk = word_id)
e = word.choice_set.get(perspective ='East')
w = word.choice_set.get(perspective ='West ')
b = word.choice_set.get(perspective ='Both')
data_string =`e.votes` +`w.votes` +`b.votes`

chart = BarChartDrawing(data = [e.votes,w.votes,b.votes],title = word.word_text)
binaryStuff = chart.asString('gif')

返回render_to_response ('templates / myapp / barChart.html',{'word':word)

#urls.py
url(r'^(?P< pk> \d +)/ barchart / $',DetailView.as_view(model = Word,
template_name ='templates / myapp / barChart.html'),name ='barChart')

#barchart.html
{%extendstemplates / myapp / base.html%}

{%block title%} {{block.super}}:{{word.word_text}} {%endblock title% }

{%block content%}

< div>< img src ={{binaryStuff}}/>< / div>
< a href ={%url templates_home%}>返回首页< / a> ;.

{%endblock content%}

我是2周新django - 如果我没有找到现有的文档,道歉,如果指向适当的地方,我将RTM。非常感谢!



----回答问题-------
当我添加打印binaryStuff这是我得到的: p>

     JFIF  C
2 !!22222222222222222222222222222222222222222222222222
}!1AQaq2 #B R $3br
%&'()* 456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz
w!1AQaq2 B #3R br
$4 % &'()* 56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz ? (











( -r K X E b 7<d ǭh U Q Gtk pȥ L I ө eJ DC JSo\w [[FXN +#B $ FS1 @ 8' / BAQבǽOEPEPEPEPEPEPEPEPEPEP\qx[ hcFDEoHW-HPT3V = q] E&安培; S} hLo4V + -e1j_ߴ+ JA @ÿ N i o <k
y ms _3 ƯG- ӯQƢ > v E @ / Z F} \eeag'jw; Z F} G-o3K2?伍[_(/我ܕQGVYkx_%E @ /(/ 5; -o3K / I5_i] A7< EW,߿Ƽ %Y / M\N]˥)QE | Q @ Q因子@ Q因子@ RW(S6 $ R + LT;޶[4 K,ЫFXgڥp { { } Kȭ ; E UL @ 1 8 I ]} #( Z [ lJ Yv
g $ MKB(HUUcc> YM]] QEQEQEfAE,| H\ *的Gxݟ¹OL< Q> - ?LE @ E\x 2 qT的ݮS * I_ ](QG + EL<oϮ} KG&安培Q> - ?X}梓ڸ {I 0 G Ե 덨ŨAm nX P V w E
2 uj * = - ظM QE V. kPYKnD2BҴ $ hc 0
N 4 ? Z + 5{M [Ih.6d ?甲uW_fэ_J_fэ^ MIZ〜&安培; / ^ EQEQEQE( jȭ@ | W&放大器; kC的〜(8((((( ((((S / */ * Z] QEQ @ Q @ Q @m ? x P J ^ M kK ; ( * ; P |-odƗrb-Q_HyEPEPEPZI̿M5rcݧ F} + X(((((((((大于?ȠJ ^ ( Ҩ G U Q^) QEQEQEV w E
¡ FDA jj浏览 jZ 6i1 # F` 2Ձ #9 K 5 _ V
t I &安培;!9 {的bG]uWcWҵWcW^ɋQE} Q因子@ Q因子@ Q因子@ ·K J_&安培; + _ P< ;; _24ɏv=(=`( | Q, xGZ 'h K
A k mXƤ : H 7 XW F Y V \ & - _DϪ试u_7GAW⫟Bfz7> V3 | üںu_Bf7> V3 | UںU_B F7> V3 | Uںu_Bf7> V3 | Uw5] S ^Eς{D'ZRBRg&安培;; 18 * IEM}吨{EQ]gυQ@ _ TU Q E _ _ Ur ! 3
+ 2



֗ ֗ ֗ E E C W ծ
P_5 k F5} + _5 k F5{7 z EW xQEQEQEV ÿ f`_S_OxDw-ԧhϷϊ+ GT;(( (+ 7#_F\wxkdx< Kʟ/ EW〜XQErP? EQW /] *G_TUgć((((+ OBV w E
ro CZ_ QE \ = x O]> < d XM ڹ 7 p 8 03 rZg ' j _ ] Ǜ u C W ծ
[\ 1 Z [\ 1ɿ/ OZ((((%Y / MY(j OhEWQE5趯BR {S&安培; km_0 F; G ^ } | U} C 1-4 aAEPEPEP] 0.6:+ 7#_F] X /#_ T〜HJ(( / * Z> 中ȠJ^ S; $ = aWtQEx AEPEPEP[ + (W ?G ( 7
( KW / 섖 2 W 7f
V p ] r:f i x I- } '

I want to render a reportlab barchart in a django template.

This django documentation gives excellent instructions for implementing the reportlab-chart generation within a django view, and this stackoverflow post recommends putting the chart object directly in an HttpReponse:

binaryStuff = chart.asString('gif')
return HttpResponse(binaryStuff, 'image/gif')

How can I insert the binaryStuff into a django template? Eg:

#views.py
def barChart(request, word_id):
    from charts import BarChartDrawing

    word = get_object_or_404(Word, pk=word_id)
    e = word.choice_set.get(perspective='East')
    w = word.choice_set.get(perspective='West')
    b = word.choice_set.get(perspective='Both')
    data_string = `e.votes` + `w.votes` + `b.votes`

    chart = BarChartDrawing(data =[e.votes, w.votes, b.votes], title=word.word_text)
    binaryStuff = chart.asString('gif')

    return render_to_response('templates/myapp/barChart.html', {'word': word)

#urls.py
url(r'^(?P<pk>\d+)/barchart/$', DetailView.as_view(model=Word,
    template_name='templates/myapp/barChart.html'), name='barChart')

#barchart.html
{% extends "templates/myapp/base.html" %}

{% block title %}{{ block.super}}: {{ word.word_text }}{% endblock title %}

{% block content %}

  <div><img src="{{ binaryStuff }}" /></div>
  <a href="{% url templates_home %}">Return to Home</a>.

{% endblock content %}

I am 2-weeks new to django - apologies if I have failed to find extant documentation, and I will RTM if pointed to the proper place. Many thanks!

----Response to Question------- When I add a print binaryStuff this is what i get:

����JFIF��C     
2!!22222222222222222222222222222222222222222222222222����"��    
���}!1AQa"q2���#B��R��$3br� 
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz���������������������������������������������������������������������������    
���w!1AQaq"2�B����  #3R�br�
$4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������?��(��
(��
(��
(��
(��
(��
(��
(��
(��
(��
(��
(��
(��-r���K��"X��E���b�7<d�ǭh�U�Q�Gtk�pȥ�L��I��ө�eJ�DC��J�So\��w�[��[�FX�n�+#B$fS�1@��8��'/�bA�q�בǽOEPEPEPEPEPEPEPEPEPEP\����qx�[hcFDEoHW-"��H�PT3�V=q]e&s�}hL��o��4�V+-�e1�j_ߴ��������+�j�A@y���N�i�o��<k���
�y�����ms�_3�ƯG-�ӯQƢ��>"���v��E�@�/���Z�"��f��}�\e�ea��g'�jw;?�Z�"��f��}�G�-o�3K���2�?�������N�g�[�_����(����/�i�ܕ�QG�V�Y�����kx����_��%�E�@�/���(����/���5;���-o�3K���/�I�5_i�]�a7�<e�w,��߿��Ƽ���%ÿ��/��M\�N���]˥��)���QE|��Q@Q@Q@r�w���(s�6���$������r+���<�޶��[4k,ЫFX���g���ڥ�p{���{�}�Kȭ��;��E�UL��@�1�8�I�]}��#(�Z������[�lJ�Yv��
g$��m���kB��(��H�U�U��cc�>Y�m����]]QEQEQE���f�A����E,�|��H\�*G��xݟ¹o�L<Q�>�?��-l�E��@�����E\�xٞ2�qTݮ�S*�i�_��������R��  ��?��G����W���o��$_��������R��  ��?��G����Q��o����Ϯ��}KG�&(��]��QG��+��E��L<Q�>�?��-�x��}t��Z�Eڸ���{I��0�G�����Ե���덨ŨAm���nX�P����V�������w��E
�2�uj��*=-ظM���QE��V.���kPYKnD2BҴ�$�hc�0���
N���4��?��Z�+��5{M[��Ih.6�����d����A?�u�W�_�����f�э_J��_�����f�э^�M�iz~�&/�^��E�QEQEQE����(�����jȭ��@���|����W&?��~��kC���(��8���(��(��(��(��(��(��(��(��(�S�/������*����"����/�*��z�������]�QE�Q@Q@Q@m�?����x�P�J���� �����^�M����kK�;�(����*��-�\�<�����v�F�Ì��8��tP?�5��_�V�
�����+�j�A@|��-���o����|��-���o���d�Ɨ��rb��-Q_Hy�EPEPEPZ���������I��������̿�M5rc�ݧ��F�?��}��+�X(��(��(��(��(��(��(��(��(���>"�Ƞ����J��^�����(����Ҩ�������G�U�Q^)�QEQEQEV�������w��E
¡��'�   fD��Ą����jj浏�jZ����6i1�#��"F`�2Ձ��#9��K�5��_�V�
�t�"�I��������&9�{bG�]u�W������cWҵ�W������cW��^��ɋ���QE}!�Q@Q@Q@k�?�J���_�&��+_���P<;�_2��4�ɏ�v�����"=��(��=`��(��|Q,�xGZ�'h�K  �
�A�k��mXƤ�:�H��7�XW�F�����Y�V�\�&�-��_DϪ试��u_���7�G����A�W���⫟�B�fz��7�����>���V����3���|��Uں��u_������B�f�7�����>���V����3���|��Uں��u_������B�f�7�����>���V����3���|��Uں��u_������B�f�7�����>���V����3���|��Uw�5�]�s�^�E�ϲ{���d'�ZR�B��Rg&;�18*�IE�m�}t�{eQ]gυQ@��_����TU��Q��E�_�_�Ur����!��3
����+�2
(��
(��
(��
���� �����X�����?�A�]���B�\����?�֗�wQE}A�QE���C��W�ծ�����
�ڵ�P_5�k����F5}+_5�k����F5{7���z�EW�xQEQEQEV����xw��e��i�"���%ÿ��/��M\���i����#�h�����
(��1�[�"f��`����_�S��_Ox��D�w����-���ԧ���h�Ϸ�ϊ���+�>�(��(��(��+��7�#���_�F�\w���x���k�����dx<K�"ʟ/��EW�~XQEr��P?��e��QW/]G�_����TU���gć���*(��(��(��(��+o����������bV�������w��E
�ro����CZ_�QE��\�=�x�O]>�<�d�XM�ڹ��7�?�p�8 ���03���rZg���'�j��_��]�Ǜ�uu����C��W�ծ��
��[\�����1��Z��[\�����1��ɿ�/O�����Z(����(��(��(����%ÿ��/��MY����(�����j����O��h�EW��QE����5���趯�b�R���{ſ�&k�����m_0���?��f;G�}�|U����}Q^a�AEPEPEP]����.����6:�+��7�#���_�F�]X/�#��_�T�~h�J(�������(���"����/�*��z�>"�Ƞ����J��^�s;�$=�aWtQEx�AEPEPEP[�������+������?�?�(W���?G����(��7
(��KW��/�섖2�W�7f�
V����p��]r:f�i�x�I-�����}����'
                        

这篇关于将reportlab图插入到django模板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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