简体   繁体   中英

Django: Display website name in Template/Emails without using Sites framework

I want to render my website name in django templates. Django's own docs on Sites state :

Use it if your single Django installation powers more than one site and you need to differentiate between those sites in some way.

My django app doesn't. I know I can still use it, but it seems like an overkill. I just want to pull a variable with my website's name (!= domain) in ANY template. I don't want to pass it in views either because that doesn't seem DRY enough.

Writing a custom processor seemed like a simple-enough option, but for some reason these variables aren't available in the .txt emails django-registration sends (while other variables definitely are, so I guess it's not impossible).

TIA

Edit: was asked to include code that doesn't work:

processors.py:

def get_website_name(request):
    website_name = 'SomeWebsite'
    return {'mysite_name': website_name}

Included successfully in context_processors in settings.py. It works nicely in "regular" templates, but not in emails.

Here's how I'm sending the emails, inside a change_email_view :

msg_plain = render_to_string('email_change_email.txt', context)
            msg_html = render_to_string('email_change_email.html', context)

            send_mail(
                'Email change request',
                msg_plain,
                'my@email',
                [profile.pending_email],
                html_message=msg_html,
            )

A further problem is that django-regitration further abstracts some of those views away: so when a user registers, wants to reset a password, etc...I don't even have access to the views.

Based on Django custom context_processors in render_to_string method you should pass the request to render_to_string .

msg_plain = render_to_string('email_change_email.txt', context, request=request)
msg_html = render_to_string('email_change_email.html', context, request=request)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM