使用 django 子域,它说 localhost 不属于域 example.com

Using django subdomain and it says localhost does not belong to the domain example.com

我正在使用 Django 包 django-subdomain,但我认为我没有正确配置它。

现在我正在尝试从数据库加载数据,我在终端中收到此错误

The host localhost:8000 does not belong to the domain example.com, unable to identify the subdomain for this request

我的项目中没有对 example.com 的任何引用。

这是我的子域配置:

ROOT_URLCONF = 'creativeflow.urls'
# A dictionary of urlconf module paths, keyed by their subdomain.
SUBDOMAIN_URLCONFS = {
    None: ROOT_URLCONF,  # no subdomain, e.g. ``example.com``
    'www': ROOT_URLCONF,
    'blog': ROOT_URLCONF + '.blogs',
}
SITE_ID = 1

中间件:

MIDDLEWARE_CLASSES = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'subdomains.middleware.SubdomainURLRoutingMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

我的网址:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^posts/(?P<year>\d{4})/(?P<months>\d{2}|\w{3})/(?P<day>\d{2})',
       BlogListView.as_view(paginate_by=25), name="blog-list-view"),
]

我不确定我还需要什么其他配置才能让我 use/develop 使用子域。我需要更改什么才能访问 http://localhost:8000/posts/2016/07/09 的 BlogListView?还是通过 blog.creativeflow.com/posts/2016/07/09 的实际子域更好?我怀疑后者只是对等同于 /etc/hosts/.

的 windows 的简单更改

What do I need to change so I can access the BlogListView at http://localhost:8000/posts/2016/07/09 ? Or better via the actual subdomain of blog.creativeflow.com

我设置了我的 linode,其中的子域指向不同的应用程序和站点。我通过在 "emperor" 模式下配置 NGINX 网络服务器和 uWSGI 网络应用守护进程来做到这一点。

要在本地测试 django-subdomain,adding subdomains to localhost 上的这个问题可能会有所帮助。

你为什么设置SITE_ID = 1

来自 django.contrib.site 的 Django 文档:

django.contrib.sites registers a post_migrate signal handler which creates a default site named example.com with the domain example.com. This site will also be created after Django creates the test database.

您需要为当前站点指定正确的 SITE_ID

SITE = 1 将对应于 django.contrib.site 设置的默认值 example.com

django.contrib.sites registers a post_migrate signal handler which creates a default site named example.com with the domain example.com. This site will also be created after Django creates the test database.

这存储在数据库中,因此无法仅在配置中进行设置。

要在数据库中设置它,请按照步骤here:

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

那你可以运行python manage.py dumpdata sites which produces a JSON of the data you've just loaded. Then later load it using django-admin loaddata fixture [fixture ...]。否则,您可以通过站点应用程序下的管理界面进行设置。

这将在修复之前显示为 example.org:

更改这些:

这应该可以解决问题。