V
V
Vasily Vorobyov2016-01-29 10:40:19
Django
Vasily Vorobyov, 2016-01-29 10:40:19

How to redirect to a subdomain in django?

Task:
redirect from mysite.com/shop_slug/action to shop_slug.mysite.com/action
Installed django-hosts, registered hosts.py:
from django_hosts import patterns, host

host_patterns = patterns('',
                         host(r'(?P<shop_slug>\w+)', 'shop.urls', name='shop_sub'),
                         host(r'www', 'main.urls', name='home'),
                         )

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


urlpatterns = [
    url('^(?P<slug>\w+)/$', views.ProductListView.as_view(), name='product_list'),
    url('^$', views.CreateShopView.as_view(), name='create_shop'),
    url(r'^(?P<slug>\w+)/product/(?P<productId>\d+)/$', views.ProductDetailView.as_view(),
        name="product_detail"),
    url(r'^(?P<slug>\w+)/category/(?P<category_id>\d+)/$', views.CategoryView.as_view(), name='view_category'),
]

settings.py:
ROOT_URLCONF = 'main.urls'
DEFAULT_HOST = 'home'
ROOT_HOSTCONF = 'hosts'

when I try to go to shop_slug.localhost:8000 it says it can't find the page
What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sim3x, 2016-01-29
@sim3x

serverfault.com/questions/458958/how-to-set-multip...

# echo "127.0.0.1 shop_slug.localhost" >> /etc/hosts

T
tema_sun, 2016-01-30
@tema_sun

First, you first write what you want from mysite.com/shop_slug/ to make shop_slug.mysite.com , and as a result, working with localhost, break into shop_slug.com . Those. you work with a domain not of the 3rd level, but of the 2nd. But, in general, this is not important - on the localhost, you can intercept all requests, you just need to tell the djanga what exactly it should listen to. There, in the comments, they suggested raising their dns, and this is true - in a different way, the wildcard will not be able to specify the local domain. And I think that such a config will not work with a dev server anyway.
Second, are you sure this is a good idea? It seems to me that nginx should deal with such redirection, not dzhanga.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question