Vijay
Vijay

Reputation: 974

extract email from variable in django template

I want to extract email address from {{e}}(which contains string with email) in below snippet

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

Upvotes: 0

Views: 249

Answers (2)

Rahul Gupta
Rahul Gupta

Reputation: 47846

You can create your own template filter get_email which will give the value of the email from the string passed to it.

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

Then in your template, you can access email by something like:

{{e|get_email}}

Value of e obtained after applying the get_email template filter on the string 'Email [email protected] is already assigned to this campaign' will be [email protected] assuming email is always at the 2nd place in the sentence.

Upvotes: 1

user3270602
user3270602

Reputation: 706

You can extract email using javascript:

var error = '[email protected], "assdsdf" <[email protected]>, "rodnsdfald ferdfnson" <[email protected]>, "Affdmdol Gondfgale" <[email protected]>, "truform techno" <[email protected]>, "NiTsdfeSh ThIdfsKaRe" <[email protected]>, "akasdfsh kasdfstla" <[email protected]>, "Bisdsdfamal Prakaasdsh" <[email protected]>,; "milisdfsfnd ansdfasdfnsftwar" <[email protected]>';    

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'));

Upvotes: 2

Related Questions