HttpResponse对象错误

我得到的错误视图registration.views.login没有返回HttpResponse对象。HttpResponse对象错误

我正在创建一个登录应用程序,我希望当我点击登录按钮主页显示给我。

给出然后我得到的页面错误未找到本地主机当我运行view.py:8000 /注册/ 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')

的urls.py文件如下:

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'),

,当我尝试为运行view.py:

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')

然后我得到错误视图registration.views.login没有返回HttpResponse对象。 的urls.py文件保持相同此view.py

回答:

您不需要在每种方法中加载模板; django会为你照顾的。 Django要求所有从urls.py调用的方法返回一个HttpResponse对象。

你的方法有很多条件,永远不会达到return语句。例如,如果用户不活跃?如果请求不是POST

试试这个版本中,如果任何一个条件出现故障,用户会被重定向回到注册页面:`在

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/')

回答:

样品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>

以上是 HttpResponse对象错误 的全部内容, 来源链接: utcz.com/qa/261815.html

回到顶部