python - 'method' 对象未找到 django 类型错误

标签 python django django-templates

我正在开发 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 时,会出现相同的错误。请帮忙...

最佳答案

您的问题出在以下代码行中:

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

请注意,如果 key 不存在,使用第二种语法会引发异常。

关于python - 'method' 对象未找到 django 类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32893131/

相关文章:

python - django 基本分页问题

python - OSMWidget - map 未显示在模板中 - ReferenceError : ol is not defined

python - 将 numpy 数组列表转换为 torch 张量列表

python - ulr 不唯一的 pandas.read_html 的替代方案?

python - 在 Django 中编写一个非常基本的搜索表单

python - 为 django 密码重置表单添加样式

Django 模板标签 "order of processing"

python - Django 模型在模板中不可迭代

python - 创建无需重复代码即可渲染不同模板的 Flask View

python - 具有公差的 NumPy setdiff1d - 将一个 numpy 数组与另一个数组进行比较并仅保存唯一值 - 在公差范围之外