Django: Get current site domain and URL

It'd be helpful to add this information into a context processor for easy access from your templates.

To get the current site information:

from django.contrib.sites.models import Site

site = Site.objects.get_current()

Use "site.domain" to retrieve the domain.

To get the current requested URL:

request.REQUEST.get('page') or request.META.get('PATH_INFO') or ""

The HTTP_REFERER is accessible through:

request.META.get('HTTP_REFERER', "")

And the query args:


Reminder: All of the .get() functions may return None, so either give it a default value of "" or allow for null values in your models.

Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog