2016-12-15 70 views
-2

我需要返回函数notification_view显示html代码。 我使用Response,但它将结果作为模板返回,但不以html代码形式返回。查看原始HTML代码,它返回Flask中的函数Python

from flask import Response 

def notification_view(user_id, notification_id): 
     """ View notification of user """ 
     notification = Notification.query.filter(Notification.unique_id == notification_id, 
               Notification.user_id == user_id).first_or_404() 

     return Response(notification.render('mail'), mimetype='text/html') 

notification_view功能发送在render方法,该方法在一个class Notifikation信息。

class Notification(db.Model): 
    unique_id = db.Column(db.String(255), primary_key=True) 
    user_id = db.Column(db.Integer, db.ForeignKey("user.id") 
    type = db.Column(db.String(60)) 


    def render(self, purpose): 
     notification_meta = NOTIFICATIONS.get(self.type) 
     if purpose == "mail" and "mail" in notification_meta: 
      return render_template(notification_meta["mail"], **render_data) 

我看到: enter image description here

但我需要,但我需要显示的消息

html style="padding: 0; margin: 0"> <body style="padding: 0; margin: 0"> <table width="100%" border="0" style="border-collapse: collapse; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;"> <tr> <td align="left" width="50%" valign="middle" style="background-color: #3c8dbc; height: 40px; color: white; padding-left: 10px;"> <span style="font-size:20px;">App</span> <span style="font-size:16px;"> Password Recovery</span> </td> <td align="right" width="50%" valign="middle" style="background-color: #3c8dbc; height: 40px; font-size:14px; padding-right: 10px;"><a style="color: white;" >Login to dashboard</a></td> </tr> <tr> <td colspan="2" style="padding: 10px; font-size: 16px;"> <p>Dear John Lee,</p> <p>A password reset has been requested for this account. <a h>Visit this link</a> and enter your new password. </p> <p>If you did not request a password reset, please ignore and delete this e-mail.</p> <p>Please note: The request will expire in 24 hours.</p> </td> </tr> <tr> <td colspan="2" style="padding: 10px; font-size: 16px; font-style: italic; color: #3c8dbc; "> Kind regards,<br> App Support </td> </tr> </table> </body> </html>

+0

“作为模板,而不是HTML代码”是什么意思?你究竟看到了什么? –

+0

究竟是什么问题?你做了什么没有奏效? –

+0

@DanielRoseman我已更新问题 – Mark

回答

0

mimetype

return Response(notification.render('mail'),mimetype='text/plain') 
改变 htmlplain的HTML代码