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. [零基础学IoT Pwn] 复现Netgear WNAP320 RCE

    [零基础学IoT Pwn] 复现Netgear WNAP320 RCE 0x00 前言: 这是[零基础学IoT Pwn]的第二篇,上篇我们搭好了仿真环境后,模拟运行了Netgear WNAP320固件 ...

  2. 面向个性化需求的在线云数据库混合调优系统 | SIGMOD 2022入选论文解读

    SIGMOD 数据管理国际会议是数据库领域具有最高学术地位的国际性会议,位列数据库方向顶级会议之首.近日,腾讯云数据库团队的最新研究成果入选 SIGMOD 2022 Research Full Pap ...

  3. 拥抱云原生 2.0 时代,Tapdata 入选阿里云首期云原生加速器!

      3月9日,阿里云首期云原生加速器官宣,Tapdata 突出重围,成功入选31 强,将与多家行业知名企业,携手阿里云共建云原生行业新生态,加速拥抱云原生新时代的无限潜能.   2021年,阿里云正式 ...

  4. ubuntu20.04安装测试uhttpd

    uhttpd是openwrt上运行一个高效小型Http服务,支持cgi, lua等特性.可以直接通过snap方式安装,如果是16.04,18.04或者20.04,snap已经默认安装了:如果是其它版本 ...

  5. MIT 6.824 Llab2B Raft之日志复制

    书接上文Raft Part A | MIT 6.824 Lab2A Leader Election. 实验准备 实验代码:git://g.csail.mit.edu/6.824-golabs-2021 ...

  6. MLX90640 红外热成像仪测温模块简要介绍说明

    MLX90640 红外热成像仪测温模块简要介绍说明 (1) A 型和 B 型的区别 区别主要有以下几点 视场角不同: A 型为 110*75° , B 型为 55*35° ,通俗一点讲就是 A 型是广 ...

  7. 记一次 .NET 某RFID标签管理系统 CPU 暴涨分析

    一:背景 1. 讲故事 前段时间有位朋友说他的程序 CPU 出现了暴涨现象,由于程序是买来的,所以问题就比较棘手了,那既然找到我,就想办法帮朋友找出来吧,分析下来,问题比较经典,有必要和大家做一下分享 ...

  8. Python词频分析

    Python词频分析 一.前言 在日常工作或者生活中,有时候会遇到词频分析的场景.如果是要进行词频分析,那么首先需要对句子进行分词,将句子中的单词进行切割并按照词性进行归类. 在Python中有个第三 ...

  9. 笔记本Usb接口案例

    笔记本电脑通常具备使用USB设备的功能.在生产的时候,笔记本都预留了可以插入USB设备的USB接口.但具体是什么USB设备,笔记本厂商并不关心,只要符合USB规格的设备都可以. 定义USB接口,具备最 ...

  10. Charles 某音最新版本抓包环境搭建

    本文仅供学习交流使用,如侵立删! Charles 某音最新版本抓包环境搭建 操作环境 google nexus5x Android 8.1 Magisk 24.3 Douyin 17.3 kaliLi ...