[Django]-Default redirect url in Django

2πŸ‘

βœ…

Add SUB_SITE to your settings.py:

# settings.py
SUB_SITE = "/test/"

And then in settings.py:

urlpatterns = patterns('',
  (r'^%s/' % settings.SUB_SITE, include('urls_subsite')),
)

More on it:

Another option is to use this peace of code in urls.py:

if settings.URL_PREFIX:
    prefixed_urlpattern = []
    for pat in urlpatterns:
        pat.regex = re.compile(r"^%s/%s" % (settings.URL_PREFIX[1:], pat.regex.pattern[1:]))
        prefixed_urlpattern.append(pat)
    urlpatterns = prefixed_urlpattern

More on it:

But I personly think that the first solution is much better.

πŸ‘€Igor Chubin

1πŸ‘

I don’t believe you should run a β€œsub” site within the same process by rewriting the patterns like the answer by @IgorChubin suggests, that should be an app.

If you really want to have two sites running on the same domain, even if they both use Django, run separate (virtual)servers.

If you really must do this, use the server config to rewrite a specific url and run separate processes

IMO, the obvious options would be to:

  • create a prefixed app within the project, or;
  • create a subdomain

Then possibly use the Django sites framework to merge administrative tasks for the latter option.

0πŸ‘

A simple answer I use is,

from django.http import HttpResponse, HttpResponseRedirect 
# hook for testing in local server, compatible with server deployment
def redirect_to_xkcd( request, everythingelse ):
    return HttpResponseRedirect( "/"+everythingelse, request )

and add this line to the urlpatterns

url(r'test/(?P<everythingelse>.+)', redirect_to_xkcd ),
πŸ‘€user3163089

Leave a comment