Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4599260
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:30:55+00:00 2026-05-21T23:30:55+00:00

I have applied a template filter, which extracts the domain from an email address.

  • 0

I have applied a template filter, which extracts the 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 %}

It is currently making bold all domain names. What I want to do is to make bold ONLY those email addresses with a ‘valid’ email extension (for example, only those at the domain ‘@gmail.com’). How do I apply an if or ifequal statement to do this?

For example, this is the logic I want it 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, like so —

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 %}

This works well now, but my concern is that when I need to update the models.py and tamper with the verified email list. Where would be a better place to hold this valid_emails() function, such that I can update it easily? And then how would I reference the function in the template (if different than current)? Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T23:30:56+00:00Added an answer on May 21, 2026 at 11:30 pm

    You can use the with template tag to assign email.email|domain to domain.

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

    Note that I’ve used the Django 1.3 with syntax. See the docs for earlier versions of Django.

    To follow up on Ben James’ comment, if you set a list of specified_extensions in the view, you can use in operator in your if statement.

    In the view:

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

    In the template:

    {% if domain in specified_extensions %}
    ...
    

    Update:

    I think that you have put the valid_emails method in the correct place, it belongs on the model. If you want to be able to update the list of domains without restarting the server, I suggest you store the domains in the database.

    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()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.