我正在处理Django项目,并出现以下错误
TypeError at /log_in/
'method' object is not subscriptable
Request Method: POST
Request URL: http://127.0.0.1:8000/log_in/
Django Version: 1.8.4
Exception Type: TypeError
Exception Value:
'method' object is not subscriptable
Exception Location: D:\pfd\recom\views.py in log_in, line 95
Python Executable: C:\Python34\python.exe
Python Version: 3.4.3
Python Path:
['D:\\pfd',
'C:\\Python34\\lib\\site-packages\\virtualenv-13.1.2-py3.4.egg',
'C:\\Windows\\SYSTEM32\\python34.zip',
'C:\\Python34\\DLLs',
'C:\\Python34\\lib',
'C:\\Python34',
'C:\\Python34\\lib\\site-packages']
Server time: Thu, 1 Oct 2015 22:34:00 +0530
以下是我的log_in def
def log_in(request):
context = RequestContext(request)
if request.method == 'POST':
username = request.POST.get['username','']
password = request.POST.get['password','']
user = auth.authenticate(username=username, password=password)
if user is not None:
if user.is_active:
auth.login(request,user)
return HttpResponseRedirect('/loggedin/')
else:
return HttpResponse("Inactive user.")
else:
return HttpResponseRedirect('error.html')
return render_to_response('error.html')
无法理解错误。
将request.method更改为request.POST。删除上下文时,未检测到帖子,只会执行最后一个语句。
尝试删除request.method时,会显示相同的错误。请帮忙......
答案 0 :(得分:1)
您的问题出在以下几行代码中:
username = request.POST.get['username','']
password = request.POST.get['password','']
应该是
username = request.POST.get('username','')
password = request.POST.get('password','')
或者,甚至
username = request.POST['username']
password = request.POST['password']
请注意,如果密钥不存在,使用第二种语法会引发异常。