django 微信企业号 返回text消息

时间:2023-12-26 09:02:31
from django.template import Context,Template
textTemplate="""
<xml>
<ToUserName><![CDATA[{{target}}]]></ToUserName>
<FromUserName><![CDATA[{{source}}]]></FromUserName>
<CreateTime>{{time}}</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[{% autoescape off %} {{content}} {% endautoescape %} ]]></Content>
<MsgId>{{MsgId}}</MsgId>
<AgentID>{{AgentID}}</AgentID>
</xml>
""" def response_text(self,msg):
"""
回复文字消息
"""
oneDic=self.message.__dict__
oneDic['content']=msg;
oneDic['time']=str(int(time.time()))
t=Template(self.textTemplate)
c=Context(oneDic)
rs=t.render(c) return rs

其中避免html字符串被转译

{% autoescape off %} {{content}} {% endautoescape %}