Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ValueError:(Django1.11.1 python3.6.1 pycharm)_Python_Django_Pycharm - Fatal编程技术网

ValueError:(Django1.11.1 python3.6.1 pycharm)

ValueError:(Django1.11.1 python3.6.1 pycharm),python,django,pycharm,Python,Django,Pycharm,当我尝试使用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

当我尝试使用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对象的值为none

url.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),
]
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':'用户名或者登录密码错误!'}) 
<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>
views.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),
]
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':'用户名或者登录密码错误!'}) 
<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>
index.html如下所示

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),
]
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':'用户名或者登录密码错误!'}) 
<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>

欢迎登陆点米年会发布会系统
年会签名系统登陆
欢迎来到滇米

登陆 {{error}}

尝试将测试字符串更改为大写。正如您在request.method的字符串中所看到的,它们不是小写

  • 更好 方法名称使用大写字母:

    如果request.method==“POST”:

  • 好多了 使用基于类的视图:

    类登录操作(视图): def post(自我,请求):
    你的代码


  • request.method
    大写,如

    if request.method == 'POST':
    

    request.method==“POST”
    行上的“POST”检查需要在views.py文件中使用大写字母。

    完全不同意第2点-基于类的视图有它们的用例,但它们现在比基于函数的视图“更好”(就像锤子比螺丝刀“更好”)。将http方法映射到适当的类方法,若不允许使用方法,则引发405,这就是视图所做的。在这里看不到基于类的视图的问题。当我看到模式'if request.method='XXXX'。。。我认为在基于类的视图中实现post方法更具可读性。@KePe非常感谢,它确实来自案例问题