Home > Net >  How to get domain in django template
How to get domain in django template

Time:08-20

I'm trying to build unsubscribe link in my email template but problem is i'm using seperate function in my utilites.py file to render my template and don't have access to request. This function is called by schedular in backend. I tried request.build_absolute_uri and other things but not able create the absulute link

templates

<body>
    <section  id="notify-form">
        <table id='datatable'></table>
        {{ content|safe }}

{#    <a href="{{ request.build_absolute_uri}}{% url 'delete' %}?email={{ sub_email }}">Unsubscribe</a>#}
{#    <a href="{% if request.is_secure %}https:/{% else %}http:/{% endif %}{{ request.get_host }}{{ request.build_absolute_uri }}{{ object.get_absolute_url }}{% url 'delete' %}?email={{ sub_email }}">Unsubscribe</a>#}
        <a href="{% if request.is_secure %}https://{% else %}http://{% endif %}{{ domain }}{% url 'delete' %}?email={{ sub_email }}">Unsubscribe</a>

    </section> <!-- /.tour  </body> -->

commented code is also what i tried tried using Sites framework but that gives doamin as example.com not what I expected

utility method

def send_notification(dataframe, email):
    subject = 'That’s your subject'
    from_email = '[email protected]'  # '[email protected]'
    text_content = 'That’s your plain text.'
    subscriber_email = QueryDetails.objects.get(email=email)
    domain = Site.objects.get_current().domain
    html_content = get_template('mail_template.html').render({'content': dataframe.to_html(classes=["table-bordered", "table-striped", "table-hover"]),'sub_email': subscriber_email, 'domain': domain})

Expected out put is if in local domain will be http://127.0.0.1/unsub/[email protected] if in production then http://whateverproductiondomain.com/unsub/[email protected]

But if i run the program with one of commented code in template them url generated is /unsub/[email protected] and with Sites framework it's http://example.com/unsub/[email protected] any guesses how to do it , send_notification is not getting called from views so can't pass request into it.

CodePudding user response:

I did it with SITE_URL='http://127.0.0.1/' in settings.py (you have different settings in production and in development).

So you need to pass it to the template:

from app.settings import SITE_URL

def send_notification(dataframe, email):
    subject = 'That’s your subject'
    from_email = '[email protected]'  # '[email protected]'
    text_content = 'That’s your plain text.'
    subscriber_email = QueryDetails.objects.get(email=email)
    domain = SITE_URL
    html_content = get_template('mail_template.html').render({'content': dataframe.to_html(classes=["table-bordered", "table-striped", "table-hover"]),'sub_email': subscriber_email, 'domain': domain})

And in template:

 <a href="{{ domain }}{% url 'delete' %}?email={{ sub_email }}">Unsubscribe</a>

CodePudding user response:

To get it dynamically, you can use request.get_host().

For example, in a views function, you can pass it to the context using context['host'] = request.get_host() and call the variable in templates using {{ host }}.

When I run it locally, I get 'localhost:8000' and when in production, I get e,g., 'domain.com'. You can then treat it as a string to append or prepend that URL with whatever you need to make it useful (for example, to add http:// in front or /whatever/path in the back).

I prefer this way instead of an alternative, like hard-coding the domain name in an environment variable and calling it because that's kind more messy to manage.

CodePudding user response:

Pass it through view function

domain = request.META["HTTP_HOST"]
  • Related