0

I am facing a problem while building a Django web app. I want that if a user logs into his account, his session should be stored and when he agains visits the login page ,he should be redirected to his home page. Here is my code.

Views.py

from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.contrib.auth.decorators import login_required
from django.template import RequestContext

def index(request):
    return HttpResponse("Index Page")

@login_required
    def home(request):
ctx = {}
return render_to_response('auth/home.html',ctx, context_instance = RequestContext(request))

def login_page(request):
    if request.user.is_authenticated():
        return redirect('cc_home')
    else:
    return render_to_response(request,'auth/cc.html')

Urls.py

from django.conf.urls.defaults import *
from django.contrib.auth.views import login, logout

urlpatterns = patterns('',
url(r'cc/', 'apps.auth.views.login_page', name = 'cc_login'),
url(r'logout/', logout, name = 'cc_logout'),
url(r'home/','apps.auth.views.home', name = 'cc_home'),
)

And here is my template cc.html

    <form action ="." method = POST>
        {% csrf_token %}    
        {{ form.as_p }}
        <input type = "submit" value = "login">
    </form>


</body>

home.html

{{ user }} 's profile
<a href = "{% url 'cc_logout' %}">Logout</a>

When I browse to CC url it should first asks the user's credentials. After successful login, it should redirect to the user's home url, which is running fine. But, when the user again browse to CC url (), he should be redirected it to his home page.

While Debugging, I found that it is not able to render the form when the user is not authenticated. It does not shows the Django's inbuilt form provided in the CC.html, it just shows the Login button.

How do I render the form. Kindly suggest.

Thanks

Praful Bagai
  • 16,684
  • 50
  • 136
  • 267

3 Answers3

1

But you haven't passed any form to the template to render! You need to instantiate the login form and pass it to the template context, otherwise {{ form }} doesn't refer to anything.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Thanks Daniel for replying. Thats exactly what my doubt is. How do I pass Django's inbuilt form in render_to_response? How do I instantiate it? – Praful Bagai Jul 13 '13 at 16:15
1

You should be able to use the default form if you replace;

url(r'cc/', 'apps.auth.views.login_page', name = 'cc_login'),

with;

url(r'cc/', login, {template_name: 'cc.html'}),
Sam Nicholls
  • 861
  • 4
  • 16
  • Although this makes the login_page view redundant so you'll have to redirect already logged in users another way I guess. – Sam Nicholls Jul 13 '13 at 16:18
  • Hi Sam, I have done that but that does not solves my query. It does not redirects the authenticated user to his HOME url when browsed to CC url. It instead browse to CC url itself, that not what I want. Any suggestions? – Praful Bagai Jul 13 '13 at 16:19
  • @user1162512 Try this; [Django: Redirect logged in users from login page](http://stackoverflow.com/a/2320702/2576437). It seems the trick is to pass the request to django.contrib.auth.views.login in your custom view. – Sam Nicholls Jul 13 '13 at 16:29
  • Hi Sam, I went through this thread. Applied some changes. But now it says Exception Type: TemplateDoesNotExist Exception Value: registration/login.html – Praful Bagai Jul 13 '13 at 17:03
  • I have not used login.html at any point of my code. I think its the inbuilt template. Kindly help me debugging it. Thanks – Praful Bagai Jul 13 '13 at 17:04
  • @user1162512 Yes that'll be the default template, you should be able to do `login(request, {template_name: 'cc.html'})` – Sam Nicholls Jul 13 '13 at 17:10
  • Hi again, are you sure that we'll be using the dictionary? Because on using it throws an exception saying Exception Type: AttributeError Exception Value: 'dict' object has no attribute 'render' – Praful Bagai Jul 13 '13 at 17:24
  • Any suggestions? How to recttify it? – Praful Bagai Jul 13 '13 at 17:24
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/33417/discussion-between-user1162512-and-samstudio8) – Praful Bagai Jul 13 '13 at 17:28
0

May be the error is in the login_page method. try to fix it like this def login_page(request): if request.user.is_authenticated(): return redirect('cc_home') else: return render_to_response('auth/cc.html',{} context_instance = RequestContext(request)) i believe that the request in your render_to_response cause the problem

drabo2005
  • 1,076
  • 1
  • 10
  • 16
  • Yes, you are absolutely correct. I have identified that the problem lies within the render statement only, but I have tried this earlier, unfortunately it isnt happening. Any suggestions? – Praful Bagai Jul 13 '13 at 16:21