当我只有一个域时,我在哪里为我的Django Sites框架网站设置域名?

时间:2022-12-04 08:15:20

I have a Django project for a simple blog/forum website I’m building.

我有一个Django项目,用于建立一个简单的博客/论坛网站。

I’m using the syndication feed framework, which seems to generate the URLs for items in the feed using the domain of the current site from the Sites framework.

我正在使用联合供稿框架,它似乎使用Sites框架中当前网站的域生成供稿中项目的URL。

I was previously unaware of the Sites framework. My project isn’t going to be used for multiple sites, just one.

我以前没有意识到Sites框架。我的项目不会用于多个站点,只有一个。

What I want to do is set the domain property of the current site. Where in my Django project should I do that? Somewhere in /settings.py?

我想要做的是设置当前站点的域属性。在我的Django项目中,我应该这样做吗?在/settings.py的某个地方?

4 个解决方案

#1


20  

If I understand correctly, Sites framework data is stored in the database, so if I want to store this permanently, I guess it’s appropriate in an initial_data fixture.

如果我理解正确,站点框架数据存储在数据库中,所以如果我想永久存储它,我想它在initial_data fixture中是合适的。

I fired up the Django shell, and did the following:

我启动了Django shell,并执行了以下操作:

>>> from django.contrib.sites.models import Site
>>> one = Site.objects.all()[0]
>>> one.domain = 'myveryspecialdomain.com'
>>> one.name = 'My Special Site Name'
>>> one.save()

I then grabbed just this data at the command line:

然后我在命令行中抓取了这些数据:

python manage.py dumpdata sites

And pasted it into my pre-existing initial_data fixture.

并将其粘贴到我之前存在的initial_data fixture中。

#2


3  

You can modify the Site entry in your database manually. Navigate to the table called 'django_site'. Then, you should only see one entry (row). You'll want to modify the field (column) named 'domain'.

您可以手动修改数据库中的“站点”条目。导航到名为“django_site”的表。然后,您应该只看到一个条目(行)。您需要修改名为“domain”的字段(列)。

#3


2  

The other answers suggest to manually update the site in the admin, shell, or your DB. That's a bad idea—it should be automatic.

其他答案建议手动更新admin,shell或您的数据库中的站点。这是一个坏主意 - 它应该是自动的。

You can create a migration that'll do this automatically when you run your migrations, so you can be assured it's always applied (such as when you deploy to production). This is also recommended in the documentation, but it doesn't list instructions.

您可以创建一个迁移,该迁移将在您运行迁移时自动执行此操作,因此您可以放心,它始终应用(例如,当您部署到生产时)。这也在文档中推荐,但没有列出说明。

First, run ./manage.py makemigrations --empty myapp to create an empty migration. Then add the following code:

首先,运行./manage.py makemigrations --empty myapp以创建空迁移。然后添加以下代码:

from django.db import migrations
from django.conf import settings


def update_site_name(apps, schema_editor):
    SiteModel = apps.get_model('sites', 'Site')
    domain = 'mydomain.com'

    SiteModel.objects.update_or_create(
        pk=settings.SITE_ID,
        domain=domain,
        name=domain
    )


class Migration(migrations.Migration):

    dependencies = [
        # Make sure the dependency that was here by default is also included here
        ('sites', '0002_alter_domain_unique'), # Required to reference `sites` in `apps.get_model()`
    ]

    operations = [
        migrations.RunPython(update_site_name),
    ]

Make sure you've set SITE_ID in your settings and rename the file to something suitable, such as update_site_details.py. Then run ./manage.py migrate to apply the changes :)

确保在设置中设置了SITE_ID,并将文件重命名为合适的文件,例如update_site_details.py。然后运行./manage.py migrate以应用更改:)

#4


1  

You can change it using django admin site.

您可以使用django管理站点进行更改。

Just go to 127.0.0.1:8000/admin/sites/

请转到127.0.0.1:8000/admin/sites/

#1


20  

If I understand correctly, Sites framework data is stored in the database, so if I want to store this permanently, I guess it’s appropriate in an initial_data fixture.

如果我理解正确,站点框架数据存储在数据库中,所以如果我想永久存储它,我想它在initial_data fixture中是合适的。

I fired up the Django shell, and did the following:

我启动了Django shell,并执行了以下操作:

>>> from django.contrib.sites.models import Site
>>> one = Site.objects.all()[0]
>>> one.domain = 'myveryspecialdomain.com'
>>> one.name = 'My Special Site Name'
>>> one.save()

I then grabbed just this data at the command line:

然后我在命令行中抓取了这些数据:

python manage.py dumpdata sites

And pasted it into my pre-existing initial_data fixture.

并将其粘贴到我之前存在的initial_data fixture中。

#2


3  

You can modify the Site entry in your database manually. Navigate to the table called 'django_site'. Then, you should only see one entry (row). You'll want to modify the field (column) named 'domain'.

您可以手动修改数据库中的“站点”条目。导航到名为“django_site”的表。然后,您应该只看到一个条目(行)。您需要修改名为“domain”的字段(列)。

#3


2  

The other answers suggest to manually update the site in the admin, shell, or your DB. That's a bad idea—it should be automatic.

其他答案建议手动更新admin,shell或您的数据库中的站点。这是一个坏主意 - 它应该是自动的。

You can create a migration that'll do this automatically when you run your migrations, so you can be assured it's always applied (such as when you deploy to production). This is also recommended in the documentation, but it doesn't list instructions.

您可以创建一个迁移,该迁移将在您运行迁移时自动执行此操作,因此您可以放心,它始终应用(例如,当您部署到生产时)。这也在文档中推荐,但没有列出说明。

First, run ./manage.py makemigrations --empty myapp to create an empty migration. Then add the following code:

首先,运行./manage.py makemigrations --empty myapp以创建空迁移。然后添加以下代码:

from django.db import migrations
from django.conf import settings


def update_site_name(apps, schema_editor):
    SiteModel = apps.get_model('sites', 'Site')
    domain = 'mydomain.com'

    SiteModel.objects.update_or_create(
        pk=settings.SITE_ID,
        domain=domain,
        name=domain
    )


class Migration(migrations.Migration):

    dependencies = [
        # Make sure the dependency that was here by default is also included here
        ('sites', '0002_alter_domain_unique'), # Required to reference `sites` in `apps.get_model()`
    ]

    operations = [
        migrations.RunPython(update_site_name),
    ]

Make sure you've set SITE_ID in your settings and rename the file to something suitable, such as update_site_details.py. Then run ./manage.py migrate to apply the changes :)

确保在设置中设置了SITE_ID,并将文件重命名为合适的文件,例如update_site_details.py。然后运行./manage.py migrate以应用更改:)

#4


1  

You can change it using django admin site.

您可以使用django管理站点进行更改。

Just go to 127.0.0.1:8000/admin/sites/

请转到127.0.0.1:8000/admin/sites/