是否可以将Django Sites Framework与同一实例上的多个Sites一起使用? [英] Is it possible to use the Django Sites Framework with multiple Sites on the same instance?

查看:64
本文介绍了是否可以将Django Sites Framework与同一实例上的多个Sites一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经定义了多个站点,作为网站框架建议的文档.

I have defined multiple sites as the documentation of the Site Framework suggested.

我知道,如果我要运行应用程序的多个实例,并且每个实例具有不同的设置文件(不同的SITE_ID),则Django总是会知道要使用哪个网站.

I understand that if I would run mulitple instances of my application with each of them having a different settings file (different SITE_ID), Django would always know which Site to use.

我试图做的是运行一个实例,其中有多个站点可用,应根据站点的当前URL选择正确的站点.

What I was trying to do is to run a single instance, where multiple sites are available, and the right Site should be chosen depending on the current url of the site.

网站文档状态:

SITE_ID设置指定站点对象的数据库ID与该特定设置文件相关联.如果设置是省略,get_current_site()函数将尝试通过将域名与主机名进行比较来获取当前站点.request.get_host()方法.

The SITE_ID setting specifies the database ID of the Site object associated with that particular settings file. If the setting is omitted, the get_current_site() function will try to get the current site by comparing the domain with the host name from the request.get_host() method.

因此,我尝试从我的settings.py中删除SITE_ID,并希望Django如上所述检查域以找到当前站点,但是由于以下异常而失败:

So I tried to remove the SITE_ID from my settings.py and was hoping that Django would check the domain to find the current Site as stated above, howewer this fails with the following exception:

You're using the Django "sites framework" without having set the SITE_ID setting. Create a site in your database and set the SITE_ID setting or pass a request to Site.objects.get_current() to fix this error.

因此,尽管文档中提出了其他建议,但此设置似乎不可忽略

So it seems like although the documentation suggests otherwise, this setting is not ommitable

我知道,当没有可用的Request对象来查找当前Site时,使用这样的Sites Framework会导致问题,但是在我的应用程序上下文中这应该不是问题.

I understand that using the Sites Framework like this would lead to problems when there is no Request object available to find the current Site, but this should not be a problem in the context of my application.

是否可以通过仅检查应用程序的当前域而无需在设置文件中对SITE_ID进行硬编码的情况下使用Sites Framework?

Is it possible to use the Sites Framework without hard-coding the SITE_ID in the settings file by just checking the current domain of the application?

我正在将Django 1.9.9与Python 3.4.3结合使用

I am using Django Version 1.9.9 with Python 3.4.3

推荐答案

要检查当前域",您需要有一个请求-如错误消息中明确提到的那样:

To "check the current domain" you need to have a request - as clearly mentionned in the error message :

或将请求传递到Site.objects.get_current()

or pass a request to Site.objects.get_current()

否则代码将如何知道当前域"?

Else how would the code know the "current domain" ?

这篇关于是否可以将Django Sites Framework与同一实例上的多个Sites一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆