简体   繁体   English

django中SITE_ID设置的用途是什么?

[英]what is the use of SITE_ID settings in django?

When I am doing the flatpage tutorial, I was getting error for SITE_ID not being set. 当我做SITE_ID教程时,我收到未设置SITE_ID错误SITE_ID I inserted SITE_ID=1 in the settings file and everything worked fine. 我在设置文件中插入了SITE_ID=1 ,一切正常。 But I don't know what this actually means. 但是我不知道这实际上意味着什么。

I read through the django docs . 我通读了django docs but I am not totally clear what its use. 但我不清楚它的用途。 when will I use something like SITE_ID=2. 我什么时候使用SITE_ID = 2之类的东西。

On the same note, I used the following snippet in my code without actually knowing what it does: 同样,我在代码中使用了以下代码片段,但实际上并不知道它的作用:

current_site=Site.objects.get_current()

I assume this has something to do with SITE_ID but may be not. 我认为这与SITE_ID但可能与无关。

Some example to demonstrate where SITE_ID could take different values than 1 would help. 举例说明SITE_ID可以采用不同于1的值的示例将有所帮助。

It is helpful is you use your code on multiple sites, or if you share a database with another site. 如果您在多个站点上使用代码,或者与另一个站点共享数据库,这将很有帮助。 An example from the documentation : 文档中的示例:

from django.db import models
from django.contrib.sites.models import Site
from django.contrib.sites.managers import CurrentSiteManager

class Photo(models.Model):
    photo = models.FileField(upload_to='/home/photos')
    photographer_name = models.CharField(max_length=100)
    site = models.ForeignKey(Site)
    objects = models.Manager()
    on_site = CurrentSiteManager()

With this model, Photo.objects.all() will return all Photo objects in the database, but Photo.on_site.all() will return only the Photo objects associated with the current site, according to the SITE_ID setting. 使用此模型, Photo.objects.all()将返回数据库中的所有Photo对象,但是根据SITE_ID设置, Photo.on_site.all()将仅返回与当前站点关联的Photo对象。

Put another way, these two statements are equivalent: 换句话说,这两个语句是等效的:

Photo.objects.filter(site=settings.SITE_ID)
Photo.on_site.all()

From documentation : 从文档

SITE_ID Default: Not defined SITE_ID默认值:未定义

The ID, as an integer, of the current site in the django_site database table. django_site数据库表中当前站点的ID(整数)。 This is used so that application data can hook into specific sites and a single database can manage content for multiple sites. 使用它是为了使应用程序数据可以挂接到特定站点,并且单个数据库可以管理多个站点的内容。

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

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