user786
user786

Reputation: 391

HttpResponse object error

I am getting the error of The view registration.views.login didn't return an HttpResponse object.

I am making an login app and I wants that when I click on login button the home page is shown to me.

When I am running view.py as given then I am getting the error of Page not found localhost:8000/registration/home.html

from django.template import  loader
from django.shortcuts import render
from registration.models import Registration
from django.http import HttpResponse
from django.template import RequestContext


def login(request):
    t = loader.get_template('registration/login.html')

    return render(request, 'registration/login.html')

The urls.py file is as:

from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()

    urlpatterns = patterns('',
        url(r'^polls/$', 'polls.views.index'),
        url(r'^polls/(?P<poll_id>\d+)/$', 'polls.views.detail'),
        url(r'^polls/(?P<poll_id>\d+)/results/$', 'polls.views.results'),
        url(r'^polls/(?P<poll_id>\d+)/vote/$', 'polls.views.vote'),
        url(r'^admin/', include(admin.site.urls)),
        url(r'^registration/$', 'registration.views.login'),

and when I am try to run view.py as:

from django.template import  loader
from django.shortcuts import render
from registration.models import Registration
from django.http import HttpResponse
from django.template import RequestContext

def login(request):
   t = loader.get_template('registration/login.html')
   if request.method == "POST":
    username = request.POST['user_name']
    password = request.POST['password']
    user = authenticate(username=username, password=password)
    if user is not None:
      if user.is_active:
        login(request, user)
        # success
        return render_to_response(request,'registration/login.html')

then I am getting the error The view registration.views.login didn't return an HttpResponse object. The urls.py file remain same for this view.py

Upvotes: 0

Views: 843

Answers (2)

Rakesh
Rakesh

Reputation: 82765

sample auth

view.py

def loginto(request):
    if request.method == "POST":
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)
        if user is not None:
        if user.is_active:
           login(request, user)
           return render_to_response('index1.html')
        else:
           return render_to_response('error1.html')
        else:
        return render_to_response('error2.html') 
    else:
        return render_to_response('error2.html')

url.py

url(r'^login', 'mytests.views.loginto', name='loginto'),

login.html

<form method="POST" action="{% url loginto %}">
  Username: <input type="text" name="username" size="15" /><br />
  Password: <input type="password" name="password" size="15" /><br />
  <div align="center">
    <p><input type="submit" value="Login" /></p>
  </div>
</form>

Upvotes: 0

Burhan Khalid
Burhan Khalid

Reputation: 174624

You don't need to load the template in each method; django will take care of that for you. Django requires that all methods that take are called from urls.py return a HttpResponse object.

Your method has lots of conditions where the return statement will never be reached. For example, what if the user is not active? What if request is not POST?

Try this version, which redirects the user back to the registration page if any of the conditions fail:

def login(request):
   # t = loader.get_template('registration/login.html') - not needed
   if request.method == "POST":
    username = request.POST.get('user_name')
    password = request.POST.get('password')
    user = authenticate(username=username, password=password)
    if user is not None:
      if user.is_active:
        login(request, user)
        # success
        return render(request,'registration/login.html')
      else:
        # user was not active
        return redirect('registration/')
    else:
        # not a valid user
        return redirect('registration/')
   else:
       # URL was accessed directly
       return redirect('registration/')

Upvotes: 2

Related Questions