How to get a full URL using Django?

Asked

Viewed 34 times

0

Is there a Django function that concatenates a specific path with the base URL and returns a full URL?

Example:

url_completa = criar_url_completa("/meu_app/minha_view")
print(url_completa) # Resultado: https://meusite.com/meu_app/minha_view

1 answer

0


In Settings.py has a field called ALLOWED_HOSTS, it can be placed the site address in various forms.

EX: ALLOWED_HOSTS = ['https://meusite.com' , 'meusite.com']

Then go to your view where you want to get your base url and import the Settings:

from django.conf import settings

url_completa = criar_url_completa("{}/meu_app/minha_view".format(ALLOWED_HOSTS[0]) 
print(url_completa) # Resultado: https://meusite.com/meu_app/minha_view

Browser other questions tagged

You are not signed in. Login or sign up in order to post.