1.编写html模板文件,会使用Django的标签
a.html:
<tbody>
{% block table_head %}#模板块,可以被子模板替换
{% autoescape off %}#自动转义关闭,相关文章参考:http://dikar.iteye.com/blog/305977
{{table_body}}#后台传入的值
{% endautoescape %}
{% endblock %}
</tbody>
b.html:
{% extends "a.html" %}#可以继承自父类的模板
{% block table_head %}
{{table_body_sub}}#以新值替换父类中的值
{% endblock %}
2.修改ulr的配置,使得能找到对应路径的服务类
urlpatterns = patterns('',
url(r'sap_comparison_history', SapComparisonHistoryTableView.as_view(), name='sap_comparison_history')
) #这样配置就使得/sap_comparison_history对应的处理类为SapComparisonHistoryTableView
3.创建对应的view处理类
class SapComparisonHistoryTableView(TemplateView):#必须继承自TemplateView类
template_name = "show_comparison_history_by_cc.html" #指明该view所对应的html页面,默认路径在templates目录下
def get_context_data(self, **kwargs): #默认调用方法,当调用该view时
# TODO need get cc_id from url
params = retrieve_param(self.request) #获取request中的数据
cc_id = params["cc_id"]
kwargs["table_body"] = self.get_table_str(cc_id) #获取数据并组装成html格式放入map中
kwargs["cc_id"] = cc_id
return kwargs #返回map供前端页面使用
def get_table_str(self, cc_id):
res = ''
for info in SapComparisonResultSnapshot.objects.filter(costcenter=cc_id).order_by('version_index'):
res += '<tr>'
res += '<td align="center">%s</td>' % info.onlybooking
res += '<td align="center">%s</td>' % info.onlysap
res += '<td align="center">%s</td>' % info.snapshot_date.strftime("%Y-%m-%d %H:%M:%S")
res += '<td align="center">%s</td></tr>' % info.version_index
return res