Django Current Domain/Path Django.How

Author avatar wrote on 30/05/2022

(In a view)

request.scheme # http or https
request.META[‘HTTP_HOST’] # example.com
request.path # /some/content/1/

(In a template)

{{ request.scheme }}
{{ request.META.HTTP_HOST }}
{{ request.path }}

Example:

Using Django sites


INSTALLED_APPS = [
    ...
    "django.contrib.sites",
    ...
]

MIDDLEWARE = [
    ...
     "django.contrib.sites.middleware.CurrentSiteMiddleware",
    ...
]



from django.contrib.sites.models import Site

current_site = Site.objects.get_current()
current_site.domain



{{request.site.domain}}
{{request.site.name}}

https://docs.djangoproject.com/en/4.0/ref/contrib/sites/

CRUDgen Django CRUD generator