从django模板中的变量中提取电子邮件

extract email from variable in django template

我想从以下代码段中的 {{e}}(包含带电子邮件的字符串)中提取电子邮件地址

{% for e in error %}
          {{e}}
    {% endfor %}

您可以使用 javascript 提取电子邮件:

var error = 'sdabhikagathara@rediffmail.com, "assdsdf" <dsfassdfhsdfarkal@gmail.com>, "rodnsdfald ferdfnson" <rfernsdfson@gmal.com>, "Affdmdol Gondfgale" <gyfanamosl@gmail.com>, "truform techno" <pidfpinfg@truformdftechnoproducts.com>, "NiTsdfeSh ThIdfsKaRe" <nthfsskare@ysahoo.in>, "akasdfsh kasdfstla" <akashkatsdfsa@yahsdfsfoo.in>, "Bisdsdfamal Prakaasdsh" <bimsdaalprakash@live.com>,; "milisdfsfnd ansdfasdfnsftwar" <dfdmilifsd.ensfdfcogndfdfatia@gmail.com>';    

function extractEmails (text)
{
    return text.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
}

$("#emails").text(extractEmails(text).join('\n'));

您可以创建自己的模板过滤器 get_email,它将根据传递给它的字符串给出电子邮件的值。

from django import template

register = template.Library()

@register.filter
def get_email(value):
    email = value.strip().split()[1] # get the 2nd word from the sentence
    return email

然后在您的模板中,您可以通过以下方式访问 email

{{e|get_email}}

在字符串 'Email test@gmail.com is already assigned to this campaign' 上应用 get_email 模板过滤器后获得的 e 的值将是 test@gmail.com 假设 email 总是在第二位在句子中。