Django中的对象HTML表示形式 [英] Object html representation in Django

查看:66
本文介绍了Django中的对象HTML表示形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个python应用程序,结果创建了一个Result对象列表。我编写了一种方法,使对象Result成为html表示形式,并显示其主要属性。

I have written a python app which as a result creates a list of Result objects. I've written a method which makes a html representation of the object Result, showing its main attributes.

class Result():

    def to_html(self):
        num_of_exp = "Number of exponentials: %s"% self.number_of_exponentials
        function = "Function: %s"% self.function
        par_of_exp = "Parameters of exponentials: <br /> %s "% pprint.pformat(self.parameters_of_exponentials)
        chunk = "Chunk: <br /> %s"% pprint.pformat(self.chunk)
        backgrnd = "Background <br /> %s" % pprint.pformat(self.background)
        raw_par_of_exp = "Raw parameters of exponentials: <br /> %s"% pprint.pformat(self.raw_parameters)
        non_phy = "Non physical solution:  %s" % pprint.pformat(self.non_physical_solution)
        user_choice = "User choice:  %s" %  pprint.pformat(self.user_choice)

        output = (function + r"<br /><br />" + num_of_exp + r"<br /><br />"+ par_of_exp + r"<br /><br />" + 
              backgrnd+ r"<br /><br />" + non_phy + r"<br /><br />"
              )

        return output

我正在使用Django为应用程序创建Web界面。

I'm using Django to make a web interface for the application.

我制作了Django模板:

I made a Django template:

...
<body>

{% for result in result_list %}
{{result.to_html}}
{% endfor %}


</body>

并添加
return render_to_response('result_pick.html',{'result_list':rp .parsed_output_data})

And added return render_to_response('result_pick.html',{'result_list': rp.parsed_output_data })

其中rp.parsed_output_data是Result对象的列表(列表的大小不固定)。

where rp.parsed_output_data is a list of Result objects (the size of the list is not fixed).

我得到的输出完全忽略了to_html()中的html标签。输出的HTML来源:

The output i get completely ignores the html tags in to_html(). Html source of the output:

Function: 98.627732*2.71828182845905**(-0.016052058*t)&lt;br /&gt;&lt;br /&gt;Number of exponentials: 1&lt;br ... ...

作为最终结果,我必须得到的结果对象的表示形式以易于阅读的格式显示,每个对象均以自己的div和下一个按钮的形式显示。因此,当用户选择结果时,他将被转移到显示其他详细信息的另一页。

What i have to get as a final result are representations of Result objects displayed in a nice human readable format, each in its own divs and form with a next button. So when the user selects a Result he is transfered to another page where additional details are shown.

我正在寻找有关如何正确,最干净地执行此操作的建议办法。任何评论表示赞赏。 Tnx

I'm looking for advices on how to do this the right and most clean way. Any comment is appreciated. Tnx

推荐答案

为了安全起见,HTML标签被忽略/自动转义,请参见此处此处

Html tags are ignored / autoescaped for safety see here and here.

正确的方法是通过Django模板系统而不是对象本身构建html输出,例如:

The right way would be constructing html output through django templating system and not within object itself e.g.:

<body>

{% for result in result_list %}
Function: {{ result.function }}<br /><br />
Number of exponentials: {{ result.number_of_exponentials }}<br /><br />
...
{% endfor %}

</body>

这篇关于Django中的对象HTML表示形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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