Django: Get current site domain and URL

1 comment

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:

request.GET.urlencode

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.

1 comment:

Leave your thoughts ...
---
If you are having trouble with copy/pasting in comments, you need to sign in or click 'Preview'. For more information about this Firefox bug, see here.

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