Django 运行报异常:AttributeError: 'str' object has no attribute 'get'
在使用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'的更多相关文章
- Django2.2报错 AttributeError: 'str' object has no attribute 'decode'
准备将 Django 连接到 MySQL,在命令行输入命令 python manage.py makemigrations 后报错: AttributeError: 'str' object has ...
- Django项目与mysql交互进行数据迁移时报错:AttributeError: 'str' object has no attribute 'decode'
问题描述 Django项目启动,当我们执行命令 python manage.py makemigrations 出现如下错误: File , in last_executed_query query ...
- python3.x运行的坑:AttributeError: 'str' object has no attribute 'decode'
1.Python3.x和Python2.X版本有一些区别,我遇到了两个问题如下: a.第一个报:mysqlclient 1.3版本不对: 解决办法:注释掉这行即可: b.第二个报:字符集的问题: 报错 ...
- Python PyInstaller 打包报错:AttributeError: 'str' object has no attribute 'items'
pyinstaller打包时报错:AttributeError: 'str' object has no attribute 'items' 网上查询,可能是setuptools比较老: 更新一下 p ...
- 【Python-遇到的Error】AttributeError: 'str' object has no attribute 'input_text'
学习类的实例化的时候遇到了AttributeError: 'str' object has no attribute 'input_text', 以下是报错的代码及修改正确的代码. class shu ...
- dnspython模块报错 AttributeError: 'CNAME' object has no attribute 'address'
有时候用到这个模块的时候会报错 AttributeError: 'CNAME' object has no attribute 'address' 如下所示 [root@ansible ch01]# ...
- py+selenium 明明定位不到元素,但却不报错或是报错AttributeError: 'list' object has no attribute 'click'【已解决】
问题:定位不到元素,但却不报错或者出现报错AttributeError: 'list' object has no attribute 'click' 如图 或者 解决方法: 将”driver ...
- 解决编码问题:AttributeError: 'str' object has no attribute 'decode'
1. 问题发现: 出现:读取文件,对其进行解码,出现错误,AttributeError: 'str' object has no attribute 'decode' 解释:属性错误,str对象不包含 ...
- 关于flask登录视图报错AttributeError: '_AppCtxGlobals' object has no attribute 'user'
在一个小程序中写了一个登录视图函数,代码如下: @app.route('/login',methods = ['GET','POST']) @oid.loginhandler def login(): ...
随机推荐
- Lua5.4源码剖析:二. 详解String数据结构及操作算法
概述 lua字符串通过操作算法和内存管理,有以下优点: 节省内存. 字符串比较效率高.(比较哈希值) 问题: 相同的字符串共享同一份内存么? 相同的长字符串一定不共享同一份内存么? lua字符串如何管 ...
- PostgreSQL 9.1 飞升之路
PostgreSQL upgrade 以升级 PostgreSQL 9.1 至 PostgreSQL 11 (跨越 9.2.9.3.9.4.9.5.9.6.10 六个大版本) 为例,本文将分享一下过去 ...
- P6622 信号传递 做题感想
题目链接 前言 在这里分享两种的做法. 一种是我第一直觉的 模拟退火.(也就是骗分) 还有一种是看题解才搞懂的神仙折半搜索加上 dp . 模拟退火 众所周知,模拟退火 是我这种没脑子选手用来骗分的好算 ...
- Android 12(S) 图像显示系统 - drm_hwcomposer 简析(下)
必读: Android 12(S) 图像显示系统 - 开篇 合成方式 合成类型的定义:/hardware/interfaces/graphics/composer/2.1/IComposerClien ...
- 全国30m精度二级分类土地利用数据
数据下载链接:数据下载链接 引言 全国土地利用数据产品是以Landsat TM/ETM/OLI遥感影像为主要数据源,经过影像融合.几何校正.图像增强与拼接等处理后,通过人机交互目视解译的方法,将全国 ...
- 一个思维导图,带你深入理解 Linux 网络
说明 思维导图 目录 内核接收网络包 内核与用户进程协作 内核发送网络包 TCP 连接 网络性能优化 GitHub 项目 说明 经朋友推荐发现一本好书:<深入理解 Linux 网络>,本文 ...
- 在矩池云使用Disco Diffusion生成AI艺术图
在 Disco Diffusion 官方说明的第一段,其对自身是这样定义: AI Image generating technique called CLIP-Guided Diffusion.DD ...
- 小白对Java的呐喊
1 public class Hello{ 2 public static void main(string[] args){ 3 System.out.print("hello world ...
- SQLZOO练习二--SELECT from Nobel Tutorial
We continue practicing simple SQL queries on a single table. This tutorial is concerned with a table ...
- elastsearch整合springboot
文档地址: https://www.baeldung.com/elasticsearch-java