How to force Django URL Manager to use subdomain?

I have a vague idea on how to solve this problem, but actually need to click :)

I have a Django application working with apache (mod_wsgi). Today's URLs look like this: http://site.com/category/A/product/B/

What I would like to do is: http://A.site.com/product/B

This means that the URL manager must somehow pick up the value found in the subdomain, and understand the context of this, and not look only at the path. I see two approaches:

  • Use .htaccess and rewrite so a.site.com will rewrite . Not sure if this is a trick since I don’t quite understand what will the django URL manager infrastructure see in this case?
  • Understanding how the URL manager works, I can write a filter that considers valid subdomains and provides this in a rewritten format for the url manager code.

Any hints or solutions are greatly appreciated! <b> Thanks.

+3
source share
3 answers

Have you looked at django.contrib.sites? I think a combination of this, installing SITE_IDin yours settings.pyand having one WSGI file per “site”, can take care of things.

The EDIT: -v .

django.contrib.sites Django. (django.contrib.sites.models.Site), domain name. , , name , , . domain - , URL-.

SITE_ID settings.py id . settings.py 1 ( ). , .

, name domain Site, , settings.py, ( ?). , , - :

SITE_ID = int(os.environ.get('SITE_ID', 1)

WSGI - :

os.environ['SITE_ID'] = 2

. WSGI , , SITE_ID Apache. , .

, Site ForeignKey ManyToManyField, (, ) .

+1

Mikes , ( ) , , Django.

, process_request() urlconf () URL-, .

URL- URL- : http://gw.tnode.com/0483-Django/

+1

Try adding a substitution subdomain: usually *.

0
source

Source: https://habr.com/ru/post/1752364/


All Articles