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 7638483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:11:20+00:00 2026-05-31T08:11:20+00:00

Inside a Django view, I create a subject like that: subject = _(u%(user)s has

  • 0

Inside a Django view, I create a subject like that:

subject = _(u"%(user)s has posted a comment") % { 'user': user }

Then I pass this subject to a function, which handles email notifications:

send_notifications(request, subject, url)

In send_notifications, I iterate over all subscriptions and send emails. However, each user can have a different language, so I activate the user’s language dynamically via Django’s activate:

def send_notifications(request, subject, url):
    from django.utils.translation import activate
    for s in Subscription.objects.filter(url=url):
        activate(s.user.userprofile.lang)
        send_mail(subject, render_to_string('notification_email.txt', locals()), settings.SERVER_EMAIL, [s.user.email])

The template gets rendered in the correct language of each user. However, the subject is passed as an evaluated and translated string to send_notifications and thus, is not translated.

I played around with lazy translations and lambda functions as parameters, but without success. Any help appreciated 🙂

  • 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-31T08:11:21+00:00Added an answer on May 31, 2026 at 8:11 am

    Instead of passing the translated subject, just pass it non translated:

    subject = '%(user)s has posted a comment'
    context = {'user': user}
    
    def send_notifications(request, subject, url, context):
        from django.utils.translation import activate
        for s in Subscription.objects.filter(url=url):
            activate(s.user.userprofile.lang)
            send_mail(_(subject) % context, render_to_string('notification_email.txt', locals()), settings.SERVER_EMAIL, [s.user.email])
    

    If you’re not going to personalize the contents per user, then you might as well limit the number of renderings because that’s a little confusing:

    # also do your imports at the top to catch import issues early
    from django.utils.translation import activate
    from django.utils.translation import ugettext as _
    
    def send_notifications(request, url, 
        translatable_subject, context,
        body_template='notification_template.txt'):
        previous_lang = None
        for s in Subscription.objects.filter(url=url).order_by('user__userprofile__lang'):
            if s.user.userprofile.lang != previous_lang:
                activate(s.user.userprofile.lang)
                subject = _(translatable_subject) % context
                body = render_to_string(body_template, locals())
            send_mail(subject, body, settings.SERVER_EMAIL, [s.user.email])
            previous_lang = s.user.userprofile.lang
    

    As such, it is much more obvious that you’re not going to render emails per usage.

    This slight rewrite should make you doubt about the original choice of a couple of names (locals, notification_template).

    The above sample code is barely an “educated guess” and you should double check it and make sure you understand everything before you paste it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In django, I can render a html template from a view action like this:
[Django 1.0.2] I have a view set up like this: (r'^redirect/(?P<object_id>\d+)/(?P<url>.*)/$', 'django.views.generic.simple.redirect_to', {'content_type': SiteType},
Symfony has very useful feature - view component, this is small action code and
I want to create a view that is able to show a ModelForm for
I'm trying to refactor a fairly hefty view function in Django. There are too
I would like to know if I can display a view inside another view
I am trying to create a django form . Like as follows class AForm(form.Form):
I want to check the useragent using Django inside my template. I know this
Inside a django template I'm trying to call the split function on one of
In a framework such as Django, I'd imagine that if a user lands on

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.