Django'Sites'模型-什么是SITE_ID = 1',为什么? [英] Django 'Sites' Model - what is and why is 'SITE_ID = 1'?

查看:315
本文介绍了Django'Sites'模型-什么是SITE_ID = 1',为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Site Django模型。

I am trying to work with Sites Model of Django.

我不太明白为什么 SITE_ID 应该是 SITE_ID = 1

I dont quite understand why SITE_ID should be SITE_ID = 1.

在文档中:


ID的整数形式django_site数据库
表中的当前站点。使用它是为了使应用程序数据可以挂接到特定的
网站,并且单个数据库可以管理多个网站的内容。

The ID, as an integer, of the current site in the django_site database table. This is used so that application data can hook into specific sites and a single database can manage content for multiple sites.

为什么 1 ?什么是当前站点?这在文档中没有明确解释。

why 1? what is the current site? this is not clearly explained in the docs.

让我说,我有 www.somecoolsite.com 和其他一些子域,例如 www .wow.coolsite.com www.awesome.coolsite.com

lets say, I have www.somecoolsite.com and some other subdomains like www.wow.coolsite.com and www.awesome.coolsite.com

I想要根据域名呈现不同的内容。

I want to render different content depending on domain name.

我的问题是,或者更好,是:

my question is, or better, are:


  1. 我是否必须将所有这些域添加到数据库的 Site 表中?

  2. 如果是这样,如何在设置中设置 SITE_ID ?我是否必须设置所有ID,例如 SITE_ID = 1 SITE_ID = 2 ..等?

  3. 当前站点 SITE_ID = 1 有什么关系?

  1. Do I have to add all those domains into Sites Table in DB?
  2. if so, how should I set SITE_ID in settings? Do I have to set all ids like SITE_ID = 1, SITE_ID = 2.. etc?
  3. what does current site has to do with SITE_ID = 1?

我在这里有点困惑。

我认为,每个站点(例如 www.wow.coolsite.com )应该是一个单独的Django项目,因此他们可以拥有自己的settings.py?并在每个这些settings.py中,我将在网站表中设置该页面的ID?但是然后有很多Django项目对我来说也没有意义。

I thought, each Site (e.g. www.wow.coolsite.com) should be a separate django project so that they can have their own settings.py? and in each of those settings.py's, I will set the id of that page from Sites table? but then there are many django projects which also doesnot make sense to me.

推荐答案

Django是由一组在报纸上开发的脚本创建的,用于在多个域上发布内容。使用一个单一的内容库。

Django was created from a set of scripts developed at a newspaper to publish content on multiple domains; using one single content base.

这是站点模块的所在。其目的是标记要在不同域显示的内容。

This is where the "sites" module comes in. Its purpose is to mark content to be displayed for different domains.

在以前的django版本中, startproject 脚本会自动添加 django.contrib.sites 应用程序安装到 INSTALLED_APPS ,并且当您执行 syncdb 时,网址为 example.com 已添加到您的数据库中,由于这是第一个站点,因此其ID为 1 ,这就是设置的来源。

In previous versions of django, the startproject script automatically added the django.contrib.sites application to INSTALLED_APPS, and when you did syncdb, a default site with the URL example.com was added to your database, and since this was the first site, its ID was 1 and that's where the setting comes from.

请记住,从1.6开始,默认情况下未启用此框架。因此,如果需要它,必须启用它

Keep in mind that starting from 1.6, this framework is not enabled by default. So if you need it, you must enable it

SITE_ID 设置可为您的项目设置默认站点。因此,如果您不要 t指定一个站点,这就是它将要使用的站点。

The SITE_ID setting sets the default site for your project. So, if you don't specify a site, this is the one it will use.

因此为不同的域配置应用程序:

So to configure your application for different domains:


  1. 启用网站框架

  2. 将默认网站从 example.com 更改为您的默认域。您可以从django shell或从管理员执行此操作。

  3. 将要发布其内容的其他站点添加到站点应用程序。同样,您可以像其他任何应用程序一样从django shell或从管理员执行此操作。

  4. 在对象 site =对象中向Site模型添加外键models.ForeignKey(Site)

  5. 添加站点管理器 on_site = CurrentSiteManager()

  1. Enable the sites framework
  2. Change the default site from example.com to whatever your default domain is. You can do this from the django shell, or from the admin.
  3. Add your other sites for which you want to publish content to the sites application. Again, you can do this from the django shell just like any other application or from the admin.
  4. Add a foreign key to the Site model in your object site = models.ForeignKey(Site)
  5. Add the site manager on_site = CurrentSiteManager()

现在,当您要过滤默认站点或特定站点的内容时:

Now, when you want to filter content for the default site, or a particular site:

foo = MyObj.on_site.all() # Filters site to whatever is `SITE_ID`
foo = MyObj.objects.all() # Get all objects, irrespective of what site
                          # they belong to

文档有完整的示例。

这篇关于Django'Sites'模型-什么是SITE_ID = 1',为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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