mortymacs
mortymacs

Reputation: 3736

Default redirect url in Django

I have a Django project in /test

WSGIScriptAlias /test /var/www/test/test/django.wsgi

It works fine , but in redirect page , like logout -> login , it goes to :

127.0.0.1/accounts/login

So I don't have this url , it must redirect to this instead:

127.0.0.1/test/accounts/login

I set this is settings.py:

LOGIN_URL = "/test/accounts/login/"

And my urls.py:

url(r'^accounts/login/$',login,name="login"),

and it doesn't work again.

How can I solve it? Thanks

Upvotes: 3

Views: 1843

Answers (3)

Igor Chubin
Igor Chubin

Reputation: 64563

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.

Upvotes: 2

user3163089
user3163089

Reputation: 1

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 ),

Upvotes: 0

Hedde van der Heide
Hedde van der Heide

Reputation: 22439

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.

Upvotes: 1

Related Questions