Where do I set the domain for my Django Sites framework site, when I only have one?

那年仲夏 提交于 2019-11-29 07:05:31

问题


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

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.

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

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?


回答1:


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.

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

>>> 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.




回答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.

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:

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 :)




回答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'.




回答4:


You can change it using django admin site.

Just go to 127.0.0.1:8000/admin/sites/



来源:https://stackoverflow.com/questions/12289148/where-do-i-set-the-domain-for-my-django-sites-framework-site-when-i-only-have-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!