2

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 base (is that the correct term?) 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.

Hack-R
  • 22,422
  • 14
  • 75
  • 131
  • Possible duplicate of [Django URL Redirect](https://stackoverflow.com/questions/14959217/django-url-redirect) – Preston Dec 04 '18 at 13:58

3 Answers3

2

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')
Hack-R
  • 22,422
  • 14
  • 75
  • 131
Sagar Ramachandrappa
  • 1,411
  • 12
  • 18
  • Thank you. This sounds correct, let me try it. +1. that first like is part of urlpatterns, right? Currently there's a `urls.py` in the main folder, but no `views.py` there. There's a `views.py` in the `djangoapp` folder. Do I make a new `views.py` for this in the main folder? – Hack-R Nov 11 '16 at 22:51
  • I just tried it by creating a new `views.py` in that main folder but it's not seeming to work for me... I will come back to this tomorrow. thanks! – Hack-R Nov 11 '16 at 22:56
  • Glad it helped :) It's okay to write it in djangoapp views file as well but make sure you are importing the right file in urls.py. – Sagar Ramachandrappa Nov 11 '16 at 22:56
  • make sure you import your view class in urls.py – Sagar Ramachandrappa Nov 11 '16 at 22:57
  • Please check the updated answer. I have added another method. – Sagar Ramachandrappa Nov 11 '16 at 23:04
  • It works!! I just had to make a tiny 1 character change to the 2nd method. I removed the `/` in `r'^/$'` – Hack-R Nov 14 '16 at 15:37
1
<meta http-equiv="refresh" content="0; url=http://example.com/" />

Note: Place it in the head section.

Additionally for older browsers if you add a quick link in case it doesn't refresh correctly:

<p><a href="http://example.com/">Redirect</a></p>

Will appear as

Redirect

This will still allow you to get to where your going with an additional click.

  • Thanks. That's how you would redirect an HTML page, but where would I put this in a Django project? – Hack-R Nov 11 '16 at 22:40
1

easiest is probably to add a decorator to your index to force a login. But then they would be redirected to that page after login, and I dont know if that is usefull in your case.

#view.py
from django.contrib.auth.decorators import login_required

@login_required
def yourindex(request):
   ...

or if using class based views:

#urls.py
from django.contrib.auth.decorators import login_required
...
   url(r'^$' , login_required(Myindex.as_view()) , name='index'),
Alex
  • 5,759
  • 1
  • 32
  • 47