Template Filter + if tag

I applied a template filter that retrieves a domain from an email address. In the template file, I have this code:

{% for email in user_list %}

<p> 
{{email.email}} corresponds to this domain: 

<b> {{email.email|domain}} </b>
</p>

{% endfor %}

Currently, bold domain names are highlighted. What I want to do is to make bold ONLY these email addresses with a "valid" email extension (for example, only in the @ gmail.com domain). How to apply if or ifequal statement for this?

For example, this is the logic I want to have -

{% for email in user_list %}

<p> 
{{email.email}} corresponds to this domain: 

    {% if domain = 'specified extension' %}
    <b> {{email.email|domain}} </b>
    {% else %}
    {{ email.email|domain }}
    {% endif %}

</p>

{% endfor %}

Update:

OK. I got this working by creating a custom model in models.py, for example:

class Table(models.Model):
    name = models.CharField(max_length=50)
    email = models.CharField(max_length=50)
    def valid_email(self):
        verified = ['yahoo.com','gmail.com']
        domain = self.email.split('@')[1]
        return domain in verified

And in the template index.html-

{% for email in user_list %}

<p> 
{{email.email}} corresponds to this domain: 

    {% if email.valid_email %}
    <b>{{ email.email|domain}}</b>
    {% else %}
    {{ email.email|domain}}
    {% endif %}
</p>

{% endfor %}

, , models.py . valid_emails(), ? ( )? .

+3
2

with email.email|domain domain.

<p> 
{{ email.email }} corresponds to this domain: 
  {% with email.email|domain=domain %} 
    {% if domain = 'specified extension' %}
    <b>{{ domain }}</b>
    {% else %}
    {{ domain }}
    {% endif %}
  {% endwith %}
</p>

, Django 1.3 with. . Django.

, _ , in operator if.

:

specified_extensions = ['gmail.com', 'hotmail.com',]

:

{% if domain in specified_extensions %}
...

:

, valid_emails , . , .

class VerifiedDomain(models.Model):
    name = models.CharField(max_length=50, help_text="an allowed domain name for emails, e.g 'gmail.com'")

class Table(models.Model):
    name = models.CharField(max_length=50)
    email = models.CharField(max_length=50)
    def valid_email(self):
        domain = self.email.split('@')[1]
        return VerifiedDomain.objects.filter(name=domain).exists()
+11

( , "" ) , Python valid_domain(domain) ( ) :

@register.filter
def bold_valid_domains(domain, autoescape=None):
    def esc(text):
        return conditional_escape(text) if autoescape else text
    if valid_domain(domain):
        result = "<b>%s</b>" % (esc(domain),)
    else:
        result = domain
    return mark_safe(result)
bold_valid_domains.needs_autoescape = True
+2

All Articles