django

Forward root URL to specific page with Django


I took over a Django web app from a coworker who quit. Currently users of the app have to type in a very long URL to login to the app and use it, like:

http://myapp.company.com/djangoapp/mydir/login

whereas the de facto root URL of

http://myapp.company.com/

points to nothing.

So now they would like http://myapp.company.com/ to automatically forward to http://myapp.company.com/djangoapp/mydir/login

Sounds simple enough and I could do it with HTML or the Spring framework, but with Django I can't seem to figure this out. I have a feeling I need to edit urls.py but I'm not sure in what way to do so.

I have access to the Django app and the server hosting it, but there's not like a cPanel or any simplifying tool like that to manage the domain. The domain itself is set up and owned by some unknown IT person in a department far, far away.


Solution

  • Create a url pointing to django RedirectView.

    For eg.

    urls.py
    
    url(_(r'^$'), LoginRedirectView.as_view(), name='redirect-to-login')
    
    views.py
    
    class LoginRedirectView(RedirectView):
        pattern_name = 'redirect-to-login'
        def get_redirect_url(self, *args, **kwargs):
            return '/djangoapp/mydir/login'
    

    Or you can directly mention the redirect url as url attribute

    from django.views.generic.base import RedirectView
    
    url(r'^$', RedirectView.as_view(url='http://myapp.company.com/djangoapp/mydir/login'), name='login-redirect')