当我尝试使用index.html中设置的用户名和密码登录时,错误显示如下:
ValueError at /login_action/
The view sign.views.login_action didn't return an HttpResponse object. It returned None instead.
Request Method: POST
Request URL: http://127.0.0.1:8000/login_action/
Django Version: 1.11.1
Exception Type: ValueError
Exception Value:
The view sign.views.login_action didn't return an HttpResponse object. It returned None instead.
Exception Location: D:\python3.6.1\lib\site- packages\django\core\handlers\base.py in _get_response, line 198
Python Executable: D:\python3.6.1\python.exe
Python Version: 3.6.1
Python Path:
['C:\\Users\\Administrator\\guest',
'D:\\python3.6.1\\python36.zip',
'D:\\python3.6.1\\DLLs',
'D:\\python3.6.1\\lib',
'D:\\python3.6.1',
'D:\\python3.6.1\\lib\\site-packages']
Server time: Mon, 12 Jun 2017 08:29:35 +0000
从提示它说HttpResponse对象的valut是没有
urls.py如下
from django.conf.urls import url
from django.contrib import admin
from sign import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/$',views.index),
url(r'^login_action/$',views.login_action),
]
views.py如下
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return render(request,"index.html")
# 登录函数定义
def login_action(request):
if request.method == 'post':
username = request.POST.get('username','')
password = request.POST.get('password','')
if username == 'admin'and password == 'admin123':
return HttpResponse( '恭喜您,登录成功!')
else:
return render(request,'index.html',{'error':'用户名或者登录密码错误!'})
index.html如下
<html>
<head>
<title>欢迎登陆点米年会发布会系统</title>
</head>
<body>
<h1>年会签名系统登陆<br>
WELCOM TO DIAN MI</h1>
<form method="post" action="/login_action/">
<input name="username" type="text" placeholder="用户名"><br>
<input name="password" type="password" placeholder="登录密码"><br>
<button id="btn" type="submit"> 登陆</button>
{{error}}<br>
</form>
</body>
</html>
答案 0 :(得分:0)
尝试将测试字符串更改为大写。正如您在https://docs.djangoproject.com/en/1.11/ref/request-response/#django.http.HttpRequest.method中看到的那样,request.method的字符串不是小写的。
答案 1 :(得分:0)
<强>更好强> 使用大写字母表示方法名称:
如果request.method =='POST':
好多了 使用基于类的视图:
类LoginAction(View):
def post(self,request):
你的代码
答案 2 :(得分:0)
将request.method
大写,例如,
if request.method == 'POST':
答案 3 :(得分:0)
第request.method == 'post'
行上的“POST”检查需要在views.py文件中为大写。