0

I've a page at / that displays a form for signup new users. When a new user registers, it will redirect to /dashboard.

But when an authenticated user go to /, it should see the /dashboard page. The question is: should I put something like this in the home() view:

if request.user.is_authenticated():
    return HttpResponseRedirect("/dashboard")

or this is an ugly method and should I return different templates from my home() relying on the user's auth status? In this case, how can I customize my URL to show /dashboard and not / ?

Fred Collins
  • 5,004
  • 14
  • 62
  • 111
  • 2
    What's wrong with the redirect? What makes you call it ugly? – S.Lott Apr 21 '11 at 20:43
  • Don't know. It feel me bad redirect every logged user from / to /dashboard. You think not? Maybe it's better use one view and return two different templates. But in this case there's a method for write /dashboard in the URL? – Fred Collins Apr 21 '11 at 20:45
  • @Fred Collins: Have you seen how web sites work? Redirects are **essential** after a form gets filled in. Why not here? – S.Lott Apr 21 '11 at 20:46
  • Yes, after a form of course. I'm not talking about after fill a form. I'm talking about a logged user who go to / and should see the user dashboard. – Fred Collins Apr 21 '11 at 20:48
  • 1
    @Fred Collins: I fail to see what the problem with a redirect is. Forms do them. **Lots** of features of sites do redirects. Post-Redirect-Get is just one example of a common redirect. Why is your one example of a redirect somehow less good than all the other redirects that are normally done? What's wrong with this one kind of redirect? – S.Lott Apr 21 '11 at 20:49
  • I'm with S.Lott on this one. Redirects are very useful. if you mean you want to keep it as / and not /dashboard then return two templates from the home view. But if it's the other way round, go with the redirect. – Eva611 Apr 21 '11 at 20:49
  • @Eva611: I'm not sure I have a position on this. I can't understand the question. – S.Lott Apr 21 '11 at 20:50
  • Ok thanks guys. You solved my doubts. – Fred Collins Apr 21 '11 at 21:06

2 Answers2

1

The redirect method is absolutely fine. Not sure what you mean by ugly. The redirect should take care of your URL issue as well.

Eva611
  • 5,964
  • 8
  • 30
  • 38
0

If I understand you correctly, you have a "dashboard" which is available only for authenticated users, so if not logged in user tries to enter it, he should be redirected to the sign up form. Right?

Have you considered using middleware? Such middleware could check if the user is logged in and if not - return the sign up form view, otherwise continue processing the request, and return the home view. Read about process_request and process_view here.

Anke
  • 8,535
  • 3
  • 22
  • 25
  • Yeah, you've understood correctly. In fact, now I use the `@login_required` decorator and all is work well. By the way, thank you for the links. – Fred Collins Apr 23 '11 at 12:28