简体   繁体   English

如何在本地测试Django的Sites Framework

[英]How to locally test Django's Sites Framework

Django has the sites framework to support multiple web site hosting from a single Django installation. Django拥有站点框架,可以通过单个Django安装支持多个网站托管。

EDIT (below is an incorrect assumption of the system) 编辑(以下是系统的错误假设)


I understand that middleware sets the settings.SITE_ID value based on a lookup/cache of the request domain. 我了解中间件根据请求域的查找/缓存设置settings.SITE_ID值。


ENDEDIT EndEdit中

But when testing locally, I'm at http://127.0.0.1:8000/ , not http://my-actual-domain.com/ 但是在本地测试时,我的网址http://127.0.0.1:8000/ ,而不是http://my-actual-domain.com/

How do I locally view my different sites during development? 如何在开发过程中在本地查看我的不同站点?

Create a separate settings.py file for every site, including an appropriate SITE_ID setting. 为每个站点创建单独的settings.py文件,包括适当的SITE_ID设置。 Of course you can use import statement to share common setting between files. 当然,您可以使用import语句来共享文件之间的公共设置。

From now on when running Django development server specify the --settings option to tell Django which site to run. 从现在开始运行Django开发服务器时,指定--settings选项告诉Django运行哪个站点。

For example (assuming you've got two setting files - settings_first.py and settings_second.py): 例如(假设您有两个设置文件 - settings_first.py和settings_second.py):

manage.py runserver --settings settings_first

will run the first site, and 将运行第一个站点,和

manage.py runserver --settings settings_second

will give you an access to the second site. 将为您提供访问第二个站点的权限。

You can also run them simultaneously, specifying different ports: 您也可以同时运行它们,指定不同的端口:

manage.py runserver 8001 --settings settings_first

manage.py runserver 8002 --settings settings_second

The above commands (run on two different consoles) will make the first website accesible under http://127.0.0.1:8001/ , and the second one under http://127.0.0.1:8002/ 上面的命令(在两个不同的控制台上运行)将使第一个网站在http://127.0.0.1:8001/下可访问,第二个在http://127.0.0.1:8002/下访问

Maybe you are mislead by the documentation. 也许你被文档误导了。 You wrote: 你写了:

I understand that middleware sets the settings.SITE_ID value based on a lookup/cache of the request domain. 我了解中间件根据请求域的查找/缓存设置settings.SITE_ID值。

This is not the case. 不是这种情况。 It works exactly the other way around. 它完全相反。 Django uses the settings.SITE_ID value to look up the correct Site object in the database. Django使用settings.SITE_ID值在数据库中查找正确的Site对象。 This returns your prefered domain and site name. 这将返回您的首选域名和站点名称。

The sites application was designed to fill the (in my opinion) rare usecase that you want to have multiple sites with the same database in the background. sites应用程序旨在填补(在我看来)罕见的用例,您希望在后台拥有多个具有相同数据库的站点。 This allows you to publish the same articles on different sites but still have the flexibility that some models are available just for a single site. 这允许您在不同的站点上发布相同的文章,但仍然具有某些模型仅适用于单个站点的灵活性。

For developing multiple projects (that doesn't actually make use of the sites framework) you don't have to specify anything special. 对于开发多个项目(实际上并没有使用站点框架),您不必指定任何特殊项目。 You can use the default SITE_ID set to 1 . 您可以将默认SITE_ID设置为1 For utilizing the admin's view on website links you can set in your development database the Site 's domain to localhost:8000 . 在网站链接使用管理员视图,您可以在开发数据库中将Site的域设置为localhost:8000

If you want to develop multiple sites using the same database (and make use of the sites framework) you must have each project with a distinct SITE_ID but the same database settings. 如果要使用同一数据库开发多个站点(并使用站点框架),则必须使每个项目具有不同的SITE_ID但具有相同的数据库设置。 The values for SITE_ID in each project on your development machine are in most cases the same as for your production servers. 开发计算机上每个项目中SITE_ID的值在大多数情况下与生产服务器相同。

仅供参考 - 我今天发布了django-dynamicsites,它有解决这个问题的工具 - https://bitbucket.org/uysrc/django-dynamicsites/src

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

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