1

I'm using the Django Package django-subdomain, and I don't think I've configured it correctly.

Now that I'm trying to load data from the DB I'm getting this error in the terminal

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

I don't have any references to example.com in my project.

Here's my subdomain config:

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:

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',
]

My urls:

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"),
]

I'm not certain of what other config I need to let me use/develop with subdomains. 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/posts/2016/07/09 ? I suspect the latter is just a simple change to the windows equivalent of /etc/hosts/.

AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173

3 Answers3

2

SITE = 1 will correspond to the default example.com set by django.contrib.site.

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.

This is stored in the DB so there's no way to set this purely in config.

To set it in the DB, follow the steps 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()

Then you can run 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 ...]. Otherwise you can set it via the admin interface, under the Site app.

List of Django Apps

This will display as example.org before it's fixed:

The existing site

Change these:

Change the two fields

That should fix the issue.

Community
  • 1
  • 1
AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173
1

Why have you set SITE_ID = 1?

From the Django Docs for django.contrib.site:

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.

You need to specify the correct SITE_ID for your current site.

Ankur Gupta
  • 707
  • 1
  • 9
  • 20
  • Quoting the docs for 'django-subdomains' `Ensure that you’ve set up your SITE_ID in your Django settings file`. What else am I supposed to set it to? – AncientSwordRage Jul 09 '16 at 20:44
  • @Pureferret Yes, you need to set the `SITE_ID`, but not necessarily to `1`. If you set it to `1`, the easiest hack is to edit the `Site` record with `id=1` from `example.com` to `localhost:8000` – Ankur Gupta Jul 09 '16 at 21:08
  • @AnkurGupta I found [this](http://stackoverflow.com/a/12289245/1075247) very helpful. – AncientSwordRage Jul 09 '16 at 21:13
  • @Pureferret That is exactly I was saying. Sorry couldn't provide an example. Good that it is resolved.. You do not necessarily need to store it in a fixture though.. Just doing the shell part would do – Ankur Gupta Jul 09 '16 at 21:16
  • To be honest the way your answer is worded, it sounded more like I needed to change `SITE_ID` to 2 or something, not set up the rest of the sites data in the DB some how. But thanks, it led me in the right direction. – AncientSwordRage Jul 09 '16 at 21:24
  • @Pureferret Yes, the answer meant what I sounded. That would have worked too. But my comment above suggested this alternative solution.. – Ankur Gupta Jul 09 '16 at 21:28
-1

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

I've set up my linode with subdomains pointed to different apps and sites. I did this by configuring NGINX webserver and uWSGI web app daemon in "emperor" mode.

To test django-subdomain locally this question on adding subdomains to localhost may help.

Community
  • 1
  • 1
John Hall
  • 556
  • 1
  • 4
  • 10