Created
December 25, 2013 06:30
-
-
Save openrijal/8120686 to your computer and use it in GitHub Desktop.
BASE_URL for Django from A BASE_URL Template Variable in Django http://www.micahcarrick.com/base-url-in-django.html via @MicahCarrick
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
context_processors.py | |
def baseurl(request): | |
""" | |
Return a BASE_URL template context for the current request. | |
""" | |
if request.is_secure(): | |
scheme = 'https://' | |
else: | |
scheme = 'http://' | |
return {'BASE_URL': scheme + request.get_host(),} | |
settings.py | |
TEMPLATE_CONTEXT_PROCESSORS = ( | |
# default context processors for Django 1.4 | |
"django.contrib.auth.context_processors.auth", | |
"django.core.context_processors.debug", | |
"django.core.context_processors.i18n", | |
"django.core.context_processors.media", | |
"django.core.context_processors.static", | |
"django.core.context_processors.tz", | |
"django.contrib.messages.context_processors.messages", | |
# context processors for 'myproject' | |
"myproject.context_processors.baseurl", | |
) | |
You can then use BASE_URL in Django templates. | |
<a href="{{ BASE_URL }}">Home</a> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment