Ajay Kumar
Ajay Kumar

Reputation: 1655

Django Apache Subdomains

I followed the steps by this reference link Multiple Sites in Django and configured in apache .but one thing how to call those domains in templates

If I type the domain names directly in the address bar it's working. But want to server through links(<a href="">Domain1</a>)

for both separate settings.py and wsgi.py as mentioned above.

I have a separate urls for both.

domain1_urls.py

from django.conf.urls import patterns, include, url

urlpatterns = patterns('',
    url(r'^$', 'domain1.views.domain1', name='domain1'),
)

domain2_urls.py

from django.conf.urls import patterns, include, url

urlpatterns = patterns('',
    url(r'^$', 'domain2.views.domain2', name='domain2'),
)

index.html

<li><a href="{% url 'domain1' %}">domain1</a></li>       
<li><a href="{% url 'domain2' %}">domain2</a></li>

The landing page is domain1. In the landing page template, there is a link for domain2. If I click that it's simply redirecting to domain1 itself. It's not working for me, Both URLs Domian1 and Domain 2 servers '/' how to differentiate both while calling in templates on a common index.html.

Please tell me what mistake I made here. If any suggestion please let me know.Thanks in advance.

Upvotes: 0

Views: 628

Answers (3)

vadimchin
vadimchin

Reputation: 1497

I think you need change architecture of app. Catch domain info on view level. For example:

from django.views.generic import View, TemplateView


class DomainMixin(View):
    def get_template_names(self):
        return ['{domain}/{template_name}'.format(domain=self.current_domain, template_name=self.template_name)]

    def dispatch(self, request, *args, **kwargs):
        self.current_domain = request.META['HTTP_HOST']
        return super(DomainMixin, self).dispatch(request, *args, **kwargs)

class IndexView(DomainMixin, TemplateView):
    template_name = 'my_app/index_view.html'

    def get_context_data(self, **kwargs):
        context = super(IndexView, self).get_context_data(**kwargs)

        ### domain specific logic ###
        if self.current_domain == '':
            pass

        return context

Upvotes: 0

GwynBleidD
GwynBleidD

Reputation: 20539

Django sites framework is created to deal with multiple sites sharing one database and maybe some other things, it wasn't created to deal with one site spreaded across multiple domains.

To deal with it properly, i suggest django-subdomains package. It is created to deal with that exact case. It contains 4 main things: simple middleware that will detect your subdomain, middleware that inherits from previous and additionally can swap ROOT_URLCONF based on subdomain, new reverse function that will take subdomain keyword argument and do lookup for url in proper urlpatterns and new {% url %} template tag that will use new reverse function.

Upvotes: 0

Dadaso Zanzane
Dadaso Zanzane

Reputation: 6259

Your both url points to same root(/), so try as follows

in domain2_urls.py

from django.conf.urls import patterns, include, url

urlpatterns = patterns('',
    url(r'^domain2/$', 'domain2.views.domain2', name='domain2'),
)

Upvotes: 1

Related Questions