“站点框架” 在单个 Django 实例上
我想在与网站其他部分不同的子域上提供专门的 RSS 提要。
我可以使用站点框架在单个 django 实例中使用不同的 urls.py 和 settings.py 文件吗? 或者我是否需要设置两个 apache 位置,然后在 apache conf 中设置不同的 settings.py 文件。
我需要设置两个 urls.py 文件的原因是为了避免重复的内容。 我不希望在 rss.example.com 上提供主站点,也不希望在 example.com 上访问专用提要
从单个 django 实例提供服务将是理想的选择,因为我们使用共享托管由于内存有限,打开一个仅提供 rss 的实例似乎是一种浪费。
编辑:我的结论是,具有单独的 urls.py 文件的多个实例对我来说是最简单的...但我发现这篇文章描述了如何使用单个实例来做到这一点:
http://effbot.org/zone/django-multihost.htm
解决方案:Django tupperware
我最终编写了一个框架,用于在单个 django 实例上运行站点的多个副本。
基本思想是为每个请求动态更改 SITE_ID
设置并从数据库加载备用设置。 它基于域执行此操作,并默认使用 SITE_ID = 1
(当它找不到任何内容时)
settings.py 文件中的所有设置均充当默认值,这些设置将被数据库中存储的设置覆盖对于当前站点。
它运行得很好:)并且它正在生产环境中运行 http://rootbuzz.com
I want to serve up specialized RSS feeds on a different subdomain from the rest of the site.
Can I use the sites framework to use a different urls.py and settings.py file within a single django instance. or do I need to set up two apache locations and just set the different settings.py files in the apache conf.
The reason I need to set up two urls.py files is to avoid duplicate content. I don't want the main site to be available at rss.example.com and I don't want the specialized feeds to be accessible on example.com
Serving them from a single django instance would be ideal because we're on shared hosting with limited memory, and it seems like such a waste to have an instance open that only serves up rss.
edit: I concluded that multiple instances with seperate urls.py files would be easiest for me... but I found this article describing how to do it using a single instance:
http://effbot.org/zone/django-multihost.htm
Solution: Django tupperware
I ended up writing a framework for running multiple copies of a site on a single django instance.
The basic idea is to change out the SITE_ID
setting on the fly for each request and load alternate settings from the database. It does this based on domain and uses SITE_ID = 1
by default (when it can't find anything)
All settings in the settings.py file act as defaults which are overridden by the settings stored in the database for the current site.
It works pretty well :) and it's running in production at http://rootbuzz.com
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 Stock Django,每个站点都必须有一个唯一的
settings.py
...因为 SITE_ID 是在settings.py
中定义的,并且是哪个站点处理此问题的关键要求。换句话说,SITE_ID 对于您的实例来说是全局的,因此您需要为每个站点一个实例。
如果您愿意,您可以拥有一个通用的
urls.py
,因为没有什么可以阻止您在所有网站settings.py
文件中使用相同的ROOT_URLCONF
...或者您可以为每个站点设置不同的。 在这种情况下,您可能需要包含子 URL,以防止对任何常见 URL 进行重复。您至少可以尝试从单个实例提供两种方法:
使用 apache + mod_wsgi< /a> 并使用 WSGIApplicationGroup 和/或 WSGIProcessGroup 指令。 我以前从未需要过这些,所以不能完全确定它们会按照您想要的方式工作,但无论如何,您绝对可以在守护程序模式下使用 mod_wsgi 来极大地改善内存占用。
您可以使用 Django 中间件根据请求主机名拒绝/允许 URL(请参阅 HttpRequest.get_host() 在 Django 文档中)。 就此而言,即使性能会受到轻微影响,您也可以在所有视图上放置一个装饰器来检查传入主机。
With stock Django you must have a unique
settings.py
for each site... because the SITE_ID is defined insettings.py
and is the key for which site is handling this request.In other words, SITE_ID is global to your instance and therefore you need an instance for each site.
You can have a common
urls.py
if you wish because there's nothing preventing you from using the sameROOT_URLCONF
in all your sitesettings.py
files... or you can have diffent one for each site. In this case you would want to include sub URLs to prevent repeating yourself for any common URLs.There are at least two methods you can try to serve from a single instance:
Use apache + mod_wsgi and use the WSGIApplicationGroup and/or WSGIProcessGroup directives. I've never needed these before so can't be completely sure these will work the way you want, but regardless you can definitely use mod_wsgi in daemon mode to greatly improve your memory footprint.
You can play with Django middleware to deny/allow URLs based on the request hostname (see HttpRequest.get_host() in the Django docs). For that matter, even though it would be a slight performance hit, you can put a decorator on all your views that checks the incoming host.
仅供参考 - 我发布了 django-dynamicsites,它可以帮助解决这个问题 - https://bitbucket.org/ uysrc/django-dynamicsites/src
FYI - I released django-dynamicsites which can be helpful with this issue - https://bitbucket.org/uysrc/django-dynamicsites/src