简体   繁体   English

一个Django项目-根据域呈现不同的内容

[英]one django project - render different content depending on domain

I seem to be using django's site framework. 我似乎正在使用Django的site框架。 :) 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) 我所做的是:(我有一个单独的django项目,带有一个settings.py)

  1. created 2 extra settings__domainname.py files with SITE_ID set to respective ID of Site object in db 在数据库中创建了2个额外的settings__domainname.py SITE_ID文件,其SITE_ID设置为Site对象的相应ID
  2. in 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 我正在运行开发服务器,如./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. 与往常一样,Aaand非常感谢任何反馈。

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: 您可以编辑hosts文件(在Windows中,Unix,Mac或C:/ windows / system32 / drivers / etc / hosts中的/ etc / hosts)并添加一些域,如下所示:

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/ 之后,以公开身份运行开发服务器(python manage.py runserver 0.0.0.0:8000),然后转到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. get_current_site在设置中查看SITE_ID,但是您需要获取request.META['HTTP_HOST']变量,并从中获取域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM