如何在本地测试 Django 的站点框架

发布于 2024-08-20 15:30:25 字数 498 浏览 9 评论 0原文

Django 有站点框架来支持通过单个 Django 安装托管多个网站。

编辑(下面是系统的错误假设)


我了解中间件根据请求域的查找/缓存设置 settings.SITE_ID 值。


ENDEDIT

但是在本地测试时,我位于 http://127.0.0.1:8000/,而不是 < a href="http://my-actual-domain.com/" rel="nofollow noreferrer">http://my-actual-domain.com/

如何在开发过程中本地查看我的不同站点?

Django has the sites framework to support multiple web site hosting from a single Django installation.

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.


ENDEDIT

But when testing locally, I'm at http://127.0.0.1:8000/, not http://my-actual-domain.com/

How do I locally view my different sites during development?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

用心笑 2024-08-27 15:30:25

为每个站点创建一个单独的 settings.py 文件,包括适当的 SITE_ID 设置。当然,您可以使用 import 语句在文件之间共享通用设置。

从现在开始,在运行 Django 开发服务器时指定 --settings 选项来告诉 Django 要运行哪个站点。

例如(假设您有两个设置文件 - settings_first.py 和 settings_second.py):

manage.py runserver --settings settings_first

将运行第一个站点,并

manage.py runserver --settings settings_second

为您提供对第二个站点的访问权限。

您还可以同时运行它们,指定不同的端口:

manage.py runserver 8001 --settings settings_first

manage.py runserver 8002 --settings settings_second

上述命令(在两个不同的控制台上运行)将使第一个网站可以在 http://127.0.0.1:8001/,以及 http://127.0 下的第二个。 0.1:8002/

Create a separate settings.py file for every site, including an appropriate SITE_ID setting. Of course you can use the import statement to share common setting between files.

From now on, when running Django development server specify the --settings option to tell Django which site to run.

For example (assuming you've got two setting files - settings_first.py and 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/

最单纯的乌龟 2024-08-27 15:30:25

也许您被文档误导了。你写道:

我了解中间件根据请求域的查找/缓存来设置 settings.SITE_ID 值。

事实并非如此。它的工作原理恰恰相反。 Django 使用settings.SITE_ID 值在数据库中查找正确的Site 对象。这将返回您首选的域名和站点名称。

sites 应用程序旨在满足(在我看来)罕见的用例,即您希望在后台拥有多个具有相同数据库的站点。这允许您在不同的站点上发布相同的文章,但仍然具有某些模型仅适用于单个站点的灵活性。

对于开发多个项目(实际上不使用站点框架),您不必指定任何特殊内容。您可以使用设置为 1 的默认 SITE_ID。要利用管理员的查看网站链接,您可以在开发数据库中将站点的域设置为localhost:8000

如果您想使用相同的数据库开发多个站点(并利用站点框架),则每个项目必须具有不同的 SITE_ID 但具有相同的数据库设置。大多数情况下,开发计算机上每个项目中的 SITE_ID 值与生产服务器的值相同。

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.

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. 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. 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. For utilizing the admin's view on website links you can set in your development database the Site's domain to 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. The values for SITE_ID in each project on your development machine are in most cases the same as for your production servers.

◇流星雨 2024-08-27 15:30:25

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

FYI - I released django-dynamicsites today which has facilities to solve this issue - https://bitbucket.org/uysrc/django-dynamicsites/src

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文