Reputation: 37904
I seem to be using django's site
framework. :) BUT. here what I dont understand:
How to write dynamic views to check current domain and render proper content to that domain
what I did is: (I have one single django project with single settings.py)
settings__domainname.py
files with SITE_ID
set to respective ID of Site object in dbin view:
if get_current_site(self.request).domain == "domain-usa.com":
context['allnews'] = News.objects.filter(country='USA')
elif get_current_site(self.request).domain == "domain-hun.com":
context['allnews'] = News.objects.filter(country='Hungary')
I am running the dev server like ./manage.py runserver --settings=myproj.settings_domainname
But I am hard-coding it anyways, i want my views to check and get content totally dynamically without any hardcoding.
How can I achieve this? I am trying to make the life of my coworkers (who may want create new domain thru admin) and of mine (have to hardcode in views) easy.
any guidance is greatly appreciated.
EDIT - almost the solution:.
I extended the Site Model like this:
class CustomSite(models.Model):
sites = models.OneToOneField(Site, null=True, related_name='customsite')
COUNTRY_CHOICES = (
('en', 'USA'),
('de', 'Germany'),
('es', 'Spain'),
('ru', 'Russia'),
('fr', 'French')
)
country = models.CharField(max_length=3, choices=COUNTRY_CHOICES)
def __unicode__(self):
return 'Country of {0}'.format(self.sites.domain)
and in view, I can do:
country_ofdomain = get_current_site(self.request).customsite.country
news_for_this_country = News.objects.filter(country=country_ofdomain)
Does this make sense? Aaand, as usual, any feedback is greatly appreciated.
Upvotes: 1
Views: 245
Reputation: 814
You can edit you hosts
file (/etc/hosts in unix, mac or C:/windows/system32/drivers/etc/hosts at windows) and add some domains, like this:
127.0.0.1 domain-usa.com
127.0.0.1 domain-hun.com
after this, run dev server as public (python manage.py runserver 0.0.0.0:8000) and go to url http://domain-usa.com:8000/
get_current_site
look at SITE_ID in your settings, but you need to get request.META['HTTP_HOST']
variable, and get domain from it.
Upvotes: 2