django-subdomains icon indicating copy to clipboard operation
django-subdomains copied to clipboard

How to test sub-domains in my localhost?

Open tahmidkhan opened this issue 9 years ago • 5 comments

I set up django-subdomains following this [guide][1].

I set my subdomain urlconfs as follows:

    SUBDOMAIN_URLCONFS = {
         None: 'mysite.urls',
        'www': 'mysite.urls',
        'shop': 'mysite.urls.shop',
        'blog': 'mysite.urls.blog'
    }

Everything works nicely, but I can't really test it cause when I run my app on my local host using python manage.py runserver, I can't really add the subdomains. If go to http://blog.127.0.0.1:8000, then the browser says a server cannot be found. Is there any way to set my server in such a way that allows for testing?

I also have this following line in my console: No handlers could be found for logger "subdomains.middleware"

How can I get this working? If anyone wants, my settings.py file can be found here:

Pastebin Link

tahmidkhan avatar Aug 11 '16 14:08 tahmidkhan

I came accross the same issue, and easily bypass it by modifying my hosts file (/etc/hosts/ in Unix systems). Here is part of mine:

# myproject development
127.0.0.1       myproject.dev
127.0.0.1       dashboard.myproject.dev
127.0.0.1       admin.myproject.dev

j-muller avatar Aug 25 '16 21:08 j-muller

I'm having trouble getting mine configured correctly.

Have sites enabled and site domain for "SITE_ID = 1" (db object 1 domain) set too "mysite.app"

I have these subdomains setup

ROOT_URLCONF = 'mysite.urls'
SUBDOMAIN_URLCONFS = {
    None: 'frontend.urls',  # no subdomain, e.g. ``example.com``
    'www': 'frontend.urls',
    'api': 'api.urls',
}

etc/hosts file

127.0.0.1       api.mysite.app
127.0.0.1       www.mysite.app
127.0.0.1       mysite.app

api/urls.py

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.index),
]

api/views.py

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    return HttpResponse('API')

The frontend app urls and views is identical except for returning string "FRONTEND" in the HttpResponse object.

I can tell django-subdomains is working becuase it does go to the frontend app when I hit "mysite.app:8000" vs the mysite.urls as seen in the root_url_conf. It displays "FRONTEND"

But no matter what I do I can't get "api.mysite.app:8000" to hit the api urls file to display "API"

Am I missing something? I'm very new to django. Any help is appreciated.

Thanks.

jamesh38 avatar Sep 02 '16 06:09 jamesh38

Hi @james-hoegerl ,

I'm having the exact same problem that you are having. Have you had any luck resolving it?

Thanks

kevdkav avatar Sep 10 '16 00:09 kevdkav

I did. Make sure you follow the directions on the docs. Adding the middleware before common. Then you must include Django.contrib.sites in your apps and then do run Python manage.py migrate. Finally the after adding the settings they provide in the docs to your settings file you'll need to access the database and change the value of entry 1 in the "django_sites" table to reflect what local domain you have set up. So change "example.com" to "mysite.app" or whatever you use. This is the ticket. The subdomains will not recognize if you don't have this site set and SITE = 1 in your settings.

I set this up like 4 times correctly since I posted this. So message me if you have any other problems.

jamesh38 avatar Sep 10 '16 19:09 jamesh38

python manage.py 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()
 one.id() #save this id to SIDE_ID in settings.py

then you need to modify /etc/hosts file

sudo vim /etc/hosts
127.0.0.1       api.mysite.app
127.0.0.1       www.mysite.app
127.0.0.1       mysite.app

it should work

Note: django-subdomains doesn't has support for django1.10. for that I have made a pull request... so django version should be under 1.9 for now :)

abe312 avatar Oct 31 '16 15:10 abe312