开发者

Python % character problem while manipulation (Escape char problem)

I try to send a confirmation email in Django but there is a problem with excape characters.

I have a helper function for content of mail as

def getActivationMailBody():
    email_body = "&开发者_高级运维lt;table width='100%'>
    email_body = email_body + '<p>' + '%(confirmLink)s' + '</p>'
    return email_body

And the confirmation url is embedded like

email_body = getActivationMailBody()
email_body = email_body % {'confirmLink': '%s/kullanici/onay/%s/%s'%(WEB_URL,md5.new(form.cleaned_data['email']).hexdigest()[:30], activation_key)}
msg = EmailMessage(email_subject, email_body, DEFAULT_FROM_EMAIL, [email_to]) 
msg.content_subtype="html"
res = msg.send(fail_silently=False)

However, while the confirmLink embedding I get an error as

unsupported format character ''' (0x27) at index 18

I found that the problem is caused by % character but I couldn't figure out how can I correct that.

Could you give me any suggestion ? Thanks


In a format string, a % can be escaped by doubling:

email_body = "<table width='100%%'>"

It's a little odd how you've constructed this, since getActivationEmailBody isn't returning the body of the email, but instead a format string to create the body. You might want to rename the function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜