Juan Andrade
Juan Andrade

Reputation: 336

how to build a url on django outside a template

I have an application that when the user instance is created a signal is dispatched to send an email. I am implementing a signal to send an SMS.

On my email HTML template I have this line that redirect the user, I need to send the same link on the SMS.

<a href="{{ uri }}{% url 'password-reset-and-active-account' uidb64=uid token=token %}">Aqui</a>

the parameters used there is passed through this context

context = {
    'user': user,
    'uid': urlsafe_base64_encode(force_bytes(user.pk)).decode("utf-8"),
    'token': default_token_generator.make_token(user),
    'uri': settings.ABSOLUTE_URI
}

how can I build this URL outside Django Templates, because the {% url 'password-reset-and-active-account' %} will not work outside the template right?

Upvotes: 0

Views: 109

Answers (1)

Eduardo Oliveira
Eduardo Oliveira

Reputation: 106

Well, you can use the reverse method. This is equals the url template tag but for python programming.

from django.urls import reverse

# ...
url = reverse('password-reset-and-active-account')

https://docs.djangoproject.com/en/3.2/ref/urlresolvers/#reverse

Upvotes: 1

Related Questions