28👍
✅
This has been answered extensively in the following post
There are several ways of doing it:
- As david542 described **
- Using
{{ request.get_host }}
in your template ** - Using the
contrib.sites
framework
** Please note these can be spoofed
24👍
None of these other answers take scheme into account. This is what worked for me:
{{ request.scheme }}://{{ request.get_host }}
- [Django]-PyCharm: DJANGO_SETTINGS_MODULE is undefined
- [Django]-In django, how do I sort a model on a field and then get the last item?
- [Django]-How to pass django rest framework response to html?
4👍
URL: google.com/hello
In Template:
{{ request.get_full_path() }}
return /hello
OR
{{ request.get_host() }}
return google.com
In view:
from django.contrib.sites.shortcuts import get_current_site
def home(request):
get_current_site(request)
# google.com
# OR
request.get_host()
# google.com
# OR
request.get_full_path()
# /hello
- [Django]-AccessDenied when calling the CreateMultipartUpload operation in Django using django-storages and boto3
- [Django]-Django character set with MySQL weirdness
- [Django]-Why won't Django use IPython?
1👍
You can get the request
object in your template by adding in the following TEMPLECT_CONTEXT_PROCESSOR
middleware in your settings:
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.request',
)
Here is some documentation on it. Then you can call in your template:
{{ request.META.HTTP_NAME }}
And that will give you the base url.
- [Django]-Name '_' is not defined
- [Django]-How do I install psycopg2 for Python 3.x?
- [Django]-Using django-admin on windows powershell
- [Django]-Specifying limit and offset in Django QuerySet wont work
- [Django]-Annotate a queryset with the average date difference? (django)
- [Django]-POST jQuery array to Django
Source:stackexchange.com