Technorati Tags: Python,Django,Web

在使用django.contrib.auth用户机制进行用户的验证、登录、注销操作时,遇到这个异常。

首先是写了一个登录的视图,要求如果用户登录成功,则页面跳转到用户主页(home):

from django.shortcuts import redirect
from django.template.loader import get_template
from django.http import HttpResponse
from django.contrib.auth import authenticate, login, logout
from django.contrib import messages def user_login(request): if request.method == 'POST':
loginForm = login_form.LoginForm(request.POST)
if loginForm.is_valid():
login_name = request.POST['username'].strip()
login_password = request.POST['password']
user = authenticate(request, username=login_name, password=login_password)
if user is not None:
if user.is_active:
login(request, user=user)
return '/home/'
else:
messages.error(request, '用户已被禁用。请联系网站管理员')
else:
messages.error(request, '用户不存在。')
else:
messages.error(request, '请检查输入的用户名和密码') else:
loginForm = login_form.LoginForm() template = get_template('login.html')
html = template.render(locals(), request) return HttpResponse(html)

运行后,当输入用户名和密码后,点击“登录”时,报如下错误:

Internal Server Error: /
Traceback (most recent call last):
File "d:\ProgramData\Anaconda3\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "d:\ProgramData\Anaconda3\lib\site-packages\django\utils\deprecation.py", line 116, in __call__
response = self.process_response(request, response)
File "d:\ProgramData\Anaconda3\lib\site-packages\django\middleware\clickjacking.py", line 26, in process_response
if response.get('X-Frame-Options') is not None:
AttributeError: 'str' object has no attribute 'get'
[15/Nov/2020 23:07:06] "POST / HTTP/1.1" 500 69094

之前没有使用Django的认证机制,程序运行正常,怎么使用了反倒报错了呢。网上找了很久,也没有对应的异常和解决方案。

终于,不经意间,发现了一行代码,第19行:return '/home/',本意是跳转到用户的主页,但这个写法是错误的,应该是:

return redirect('/home'/)

修改之后,正常运行没有报异常,页面也可以跳转到预期的用户主页。

另外,推荐一篇文章:django从请求到响应的过程,文章很详细地讲解了Django的运行原理。

Django 运行报异常:AttributeError: 'str' object has no attribute 'get'的更多相关文章

  1. Django2.2报错 AttributeError: 'str' object has no attribute 'decode'

    准备将 Django 连接到 MySQL,在命令行输入命令 python manage.py makemigrations 后报错: AttributeError: 'str' object has ...

  2. Django项目与mysql交互进行数据迁移时报错:AttributeError: 'str' object has no attribute 'decode'

    问题描述 Django项目启动,当我们执行命令 python manage.py makemigrations 出现如下错误: File , in last_executed_query query ...

  3. python3.x运行的坑:AttributeError: 'str' object has no attribute 'decode'

    1.Python3.x和Python2.X版本有一些区别,我遇到了两个问题如下: a.第一个报:mysqlclient 1.3版本不对: 解决办法:注释掉这行即可: b.第二个报:字符集的问题: 报错 ...

  4. Python PyInstaller 打包报错:AttributeError: 'str' object has no attribute 'items'

    pyinstaller打包时报错:AttributeError: 'str' object has no attribute 'items' 网上查询,可能是setuptools比较老: 更新一下 p ...

  5. 【Python-遇到的Error】AttributeError: 'str' object has no attribute 'input_text'

    学习类的实例化的时候遇到了AttributeError: 'str' object has no attribute 'input_text', 以下是报错的代码及修改正确的代码. class shu ...

  6. dnspython模块报错 AttributeError: 'CNAME' object has no attribute 'address'

    有时候用到这个模块的时候会报错 AttributeError: 'CNAME' object has no attribute 'address' 如下所示 [root@ansible ch01]# ...

  7. py+selenium 明明定位不到元素,但却不报错或是报错AttributeError: 'list' object has no attribute 'click'【已解决】

    问题:定位不到元素,但却不报错或者出现报错AttributeError: 'list' object has no attribute 'click' 如图  或者  解决方法:   将”driver ...

  8. 解决编码问题:AttributeError: 'str' object has no attribute 'decode'

    1. 问题发现: 出现:读取文件,对其进行解码,出现错误,AttributeError: 'str' object has no attribute 'decode' 解释:属性错误,str对象不包含 ...

  9. 关于flask登录视图报错AttributeError: '_AppCtxGlobals' object has no attribute 'user'

    在一个小程序中写了一个登录视图函数,代码如下: @app.route('/login',methods = ['GET','POST']) @oid.loginhandler def login(): ...

随机推荐

  1. SHT11和SHT21传感器

    1.传感器概述 SHT11和SHT21为瑞士Sensirion公司生产,精度和测量范围较广,但价格较高.SHT11和SHT21是具有IIC总线接口的单片全校准数字式相对湿度和温度传感器.该传感器采用独 ...

  2. warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8): No such file or directory

    1)apt-get clean && apt-get update && apt-get install -y locales 2)locale-gen en_US.U ...

  3. 牛客SQL刷题第一趴——非技术入门基础篇

    user_profile表: id device_id gender age university province 1 2138 male 21 北京大学 Beijing 2 3214 male   ...

  4. 关于 STrAduts

    \(\mathbb{No \ hay \ cosa \ mas \ feliz \ en \ el \ mundo \ que \ ver \ tu \ sonrisa \ mi \ [数据删除]}\ ...

  5. 对象数组的foreach循环操作和集合容器

    ForEach标签可以循环数组,list,map集合 采用 foreach循环遍历 ,并每次循环允许执行一次回调函数 for (容器中元素类型 临时变量: 容器变量) { System.out.pri ...

  6. CSS 上下居中和最低高度语法

    /*上下居中*/ vertical-align:center; font-size=line-height; /*最低高度*/ height:auto!important; height:400px; ...

  7. odoo14 button 事件调用python方法如何传递参数

    1 <field name="user_ids" 2 mode="kanban" 3 nolabel="1" 4 options=&q ...

  8. SpringBoot定时任务 - Spring自带的定时任务是如何实现的?有何注意点?

    通过前文我们基本梳理了定时任务体系:Timer和ScheduledExecutorService是JDK内置的定时任务方案,以及Netty内部基于时间轮实现的HashedWheelTimer,再到Qu ...

  9. 一文带你了解webrtc基本原理(动手实现1v1视频通话)

    webrtc (Web Real-Time Communications) 是一个实时通讯技术,也是实时音视频技术的标准和框架. 大白话讲,webrtc是一个集大成的实时音视频技术集,包含了各种客户端 ...

  10. 巧用Prometheus来扩展kubernetes调度器

    Overview 本文将深入讲解 如何扩展 Kubernetes scheduler 中各个扩展点如何使用,与扩展scheduler的原理,这些是作为扩展 scheduler 的所需的知识点.最后会完 ...