Inte L
Inte L

Reputation: 345

Get main domain url without hardcoding

In Django, I have many sub domains and these have a theme installed to it. The problem is now I need to implement a function that redirects to the main domain of my project. For example when pressing a link ("Go to main domain") in the sub domain theme it will take them to the main domain.

I can hard code this, but it's not really nice. So I'm looking for other solutions to this.

This is a hardcoded way in views.py:

def network_url(request):
    return redirect('https://domain.com/')

So how do I create a function that do not require to hardcode the main domain?

Upvotes: 1

Views: 165

Answers (2)

Savad KP
Savad KP

Reputation: 1654

Try this

def network_url(request):
    return redirect(request.META.get("HTTP_HOST"))

Upvotes: 0

JRodDynamite
JRodDynamite

Reputation: 12613

Just store your links in settings.py.

MAIN_DOMAIN_LINK = 'https://domain.com/'

Then, you can simply access them by importing django.conf.settings in your views:

from django.conf import settings

def network_url(request):
    return redirect(settings.MAIN_DOMAIN_LINK)

Hope this helps. Docs link.

Upvotes: 2

Related Questions