簡體   English   中英

django渲染成html之類的xml

[英]django render to xml like html

我正在嘗試通過django.shortcut.render_to_response渲染為xml,但是似乎不起作用。 只能找到一些解決方案來解決這個問題嗎?

這是代碼

class BaseResponse(object):
    """
    這個類類似於Django的通用視圖,response_name是要輸出的消息模版
    msg是用戶輸入消息的一個字典,如果要自定義消息,只要自定義context
    就可以了,消息內容格式由微信官方指定
    """
    template_name = None
    response_type = "application/xhtml+xml"

    def __init__(self, msg):
        self.msg = msg
        self.template_context = {'ToUserName': msg['FromUserName'], 'FromUserName': msg['ToUserName'],
                                 'CreateTime': time.time()}

    def get_response(self, context=None):
        self.template_context.update(context)
        res = render_to_response(self.template_name, context=self.template_context,
                                 content_type=self.response_type)
        return self.template_context

和一些XML

<xml>
    <ToUserName><![CDATA[{{ ToUserName }}]]></ToUserName>
    <FromUserName><![CDATA[{{ FromUserName }}]]></FromUserName>
    <CreateTime>{{ CreateTime }}</CreateTime>
    <MsgType><![CDATA[text]]></MsgType>
    <Content><![CDATA[{{ Content }}]]></Content>
</xml>

更新您的代碼以傳遞上下文字典並返回響應對象。 像這樣

def get_response(self, context=None):
        self.template_context.update(context)
        return render_to_response(self.template_name, self.template_context,
                                 content_type=self.response_type)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM