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. NC16462 [NOIP2015]跳石头

    NC16462 [NOIP2015]跳石头 题目 题目描述 一年一度的"跳石头"比赛又要开始了! 这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石.组委会已经选择好了两块 ...

  2. Educational Codeforces Round 128 (Rated for Div. 2) A-C+E

    Educational Codeforces Round 128 (Rated for Div. 2) A-C+E A 题目 https://codeforces.com/contest/1680/p ...

  3. LEACH分簇算法实现和能量控制算法实现

    一.前言 1.在给定WSN的节点数目(100)前提下,节点随机分布,按照LEACH算法,实现每一轮对WSN的分簇.记录前K轮(k=10)时,网络的分簇情况,即每个节点的角色(簇头或簇成员).标记节点之 ...

  4. ReentrantLock 公平锁源码 第1篇

    ReentrantLock 1 这篇还是接着ReentrantLock的公平锁,没看过第0篇的可以先去看上一篇https://www.cnblogs.com/sunankang/p/16456342. ...

  5. 👨‍💻Mybatis源码我搞透了,面试来问吧!写了134个源码类,1.03万行代码!

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言:手撸一万行! 完结撒花:4个月.20章.134个类.1.03万行代码! 22年3月初 ...

  6. JavaWEB-03-JDBC

    内容 JDBC `JDBC`简介 JDBC `JDBC`快速入门 JDBC API `JDBC API` 详解 数据库连接池 JDBC `JDBC`案例 1. JDBC入门 1.1 概述 概念 JDB ...

  7. vue 调用nginx服务跨越的问题

    server { listen 80; server_name api.xxxx.com; add_header Access-Control-Allow-Origin '*' ; proxy_set ...

  8. python+tkinter 的布局

    from tkinter import * win = Tk() win.title("布局") # #窗口标题 win.geometry("600x500+200+20 ...

  9. sql语句实现每日资格设置

    CREATE TABLE 'iDayAuth'( 'openid' VARCHAR(16) NOT NULL , 'iStamp' INT(10) NOT NULL, 'iDayAuth' SMALL ...

  10. 浩若烟海事半功倍|利用Docker容器技术构建自动化分布式web测试集群Selenium Grid

    原文转载自「刘悦的技术博客」https://v3u.cn/a_id_195 "世界上有那么多城市,城市里有那么多的酒馆,可她,却偏偏走进了我的-",这是电影<卡萨布拉卡> ...