How to Get the Domain Name of My Site Within a Django Template

How to get the domain name of my site within a Django template?

I think what you want is to have access to the request context, see RequestContext.

How do I detect the current domain name in django?

The way i did it is basically using the middleware (using session and detect the HTTP_HOST.

class SimpleMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response
# One-time configuration and initialization.

def __call__(self, request):
# Code to be executed for each request before the view (and later middleware) are called.

# sets to show Taiwan or Indo version
# sets the timezone too
http_host = request.META['HTTP_HOST']
if(http_host == 'http://www.xxx.tw'):
request.session['web_to_show'] = settings.TAIWAN
request.session['timezone_to_use'] = settings.TAIWAN_TIMEZONE
else:
request.session['web_to_show'] = settings.INDO
request.session['timezone_to_use'] = settings.INDONESIA_TIMEZONE

response = self.get_response(request)

# Code to be executed for each request/response after the view is called.

return response

Displaying my current domain in my django template not working

I solved this by using the get_current_site method.

In views:

from django.contrib.sites.shortcuts import get_current_site

my_current_domain = get_current_site(request)
return {'domain':my_current_domain}

then in template

<a href="{{ request.scheme }}://{{ domain }}{% url 'add_your_details' user.username %}" 
target="_blank">Add your details </a>

How to get the current URL within a Django template?

Django 1.9 and above:

## template
{{ request.path }} # -without GET parameters
{{ request.get_full_path }} # - with GET parameters

Old:

## settings.py
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.request',
)

## views.py
from django.template import *

def home(request):
return render_to_response('home.html', {}, context_instance=RequestContext(request))

## template
{{ request.path }}

Django access site name/url in template - views, contexts and settings

ok, so it was a wrong path after all - the reason why {{ domain }} in my template didn't work (resolved to 'localhost') was a missing line in nginx config:

  location / {
proxy_pass http://unix:/run/gunicorn.sock;
proxy_set_header Host $http_host;
}

(the proxy_set_header line, to be exact)

can't claim I understand 100% what's happening here, but at least now I can move forward; the biggest question is where this {{ domain }} directive comes from - can't tell which of the several context processors copied from a tutorial put it in:

"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",

How can I get the full/absolute URL (with domain) in Django?

Use handy request.build_absolute_uri() method on request, pass it the relative url and it'll give you full one.

By default, the absolute URL for request.get_full_path() is returned, but you can pass it a relative URL as the first argument to convert it to an absolute URL.

>>> request.build_absolute_uri()
'https://example.com/music/bands/the_beatles/?print=true'
>>> request.build_absolute_uri('/bands/?print=true')
'https://example.com/bands/?print=true'

Current way to get Home URL (Domain) in Django Template?

Ha! Nice question.

Let's break down your problem. You want some data to be available across all the templates available in your project. And also, you want to provide the value once and not repeat it across views.

Template Context Processors is the thing you are looking for.
In your settings.py file, add a new context_processor to the list of TEMPLATE_CONTEXT_PROCESSORS.

TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.media",
"django.core.context_processors.request",
"django.contrib.messages.context_processors.messages",
"your_app.context_processors.root_url"
)

Then, inside your_app, create a file named context_processors.py. This file will contain the following code.

from django.conf import settings

def root_url(request):
"""
Pass your root_url from the settings.py
"""
return {'SITE_URL': settings.ROOT_URL_YOU_WANT_TO_MENTION}

And, in each of your templates, you'll have a {{SITE_URL}} present in the context depending on the value you provide to ROOT_URL_YOU_WANT_TO_MENTION in your settings.py file.

Django sure spoils everyone. But provides the mechanisms to keep you spoilt.

Hope this solves your problem.



Related Topics



Leave a reply



Submit