Django redirect within my code without changing display url and send right status code
-
15-10-2022 - |
Pregunta
I'll like to redirect the url like my admin using subdomain without changing the url
admin.example.com
so in my middleware ill change this to
example.com/admin
but still display the url as if there was no redirect.
here is what my middleware currently looks like
from django.http import HttpResponseRedirect
from django.conf import settings
sitename = getattr(settings,'SITE_DEFAULT_DOMAIN')[:4]
class SubdomainMiddleware(object):
def process_request(self, request):
host = request.get_host()
domain_parts = host.split('.')
entry_url = "{0}://{1}{2}".format(request.META['wsgi.url_scheme'],\
host,\
request.get_full_path() )
setattr(request,'entry_url',entry_url)
if (len(domain_parts) > 2):
subdomain = domain_parts[0]
if subdomain.lower() not in ('www',sitename):
request.incoming_path = request.META['PATH_INFO']
domain = '.'.join(domain_parts[1:])
if request.META['PATH_INFO'] == '/' :
path = "{0}{1}{2}".format('/',subdomain,'/')
else:
path = "{0}{1}{2}".format('/',subdomain,request.META['PATH_INFO'])
else:
domain = host.replace('www','')
path = request.META['PATH_INFO']
redirect_path = "{0}://{1}{2}".format(request.META['wsgi.url_scheme'],domain, path)
return HttpResponseRedirect(redirect_path)
def process_response(self, request, response):
entry_url = getattr(request,'entry_url',None)
if entry_url:
response.location = entry_url
return response
No hay solución correcta
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow