TypeError at / 'AnonymousUser' object is not iterable
在使用filter的时候,出现了如下错误(python版本3.7.4,django 版本2.2.6):
错误显示在‘/’目录下,也就是默认的index视图下,其中index 视图的源码如下:
def index(request):
posts=Post.objects.filter(owner=request.user).order_by('date_added')
context={'posts':posts}
return render(request,'blogs/index.html',context)
其中第一行filter(owner=request.user)函数需要用户登录后才能使用,但此代码下没有显示登录信息,所以显示此错误,简单来说就是在没有对用户认证前使用了需要用户认证的函数。解决方法如下:
(1)使用django提供的login_required,即添加:from django.contrib.auth.decorators import login_required,然后在index试图前加上:@login_required,表示此视图需要用户登录才能访问
(2)使用is_authenticated在认证用户之前进行验证(注意,此django 版本后面不需要'()'),修改后,源码如下:
def index(request):
if request.user.is_authenticated:
posts=Post.objects.filter(owner=request.user).order_by('date_added')
context={'posts':posts}
return render(request,'blogs/index.html',context)
else :
posts=Post.objects.order_by('date_added')
context={'posts':posts}
return render(request,'blogs/index.html',context)
实现所需功能,即登录前显示所有用户的post,登录后只显示当前用户拥有的post
TypeError at / 'AnonymousUser' object is not iterable相关推荐
- TypeError: 'numpy.int64' object is not iterable ,'int' object is not iterable
想用一个list来动态地增加numpy类型数据,如下面的代码所示,发现报错TypeError: 'numpy.int64' object is not iterable a = [] b = np.a ...
- Python+selenium自动化测试:报错:TypeError: 'WebElement' object is not iterable
代码: # -*- coding: utf-8 -*-from selenium import webdriverfrom selenium.webdriver.common.by import By ...
- python nonetype_【已解决】Python程序错误:TypeError: ‘NoneType’ object is not iterable
[问题] 如下代码:def callBlogFunc(funcToCall, *paraList): funcName = funcToCall.func_name; if(blogIsNetease ...
- 成功解决TypeError int object is not iterable
成功解决TypeError: 'int' object is not iterable 目录 解决问题 解决思路 解决方法 解决问题 TypeError: 'int' object is not it ...
- Python报错TypeError: ‘User‘ object is not iterable
Python报错TypeError: 'User' object is not iterable 原因 User对象是不可迭代的,我这里是因为User并不是list,所以是不可迭代的,所以不使用迭代即 ...
- 【Python】处理 TypeError: ‘FirefoxWebElement‘ object is not iterable
编写下面的代码: from selenium import webdriverdriver = webdriver.Firefox() driver.maximize_window() driver. ...
- TypeError: 'float' object is not iterable
求心率的peason相关系数的时候遇到这个问题: TypeError: 'float' object is not iterable 解决方法: pre_label.extend(output) ty ...
- python常见错误 TypeError: 'NoneType' object is not iterable
TypeError: 'NoneType' object is not iterable 1. 将None赋给多个值时,会出现提示:TypeError: 'NoneType' object is no ...
- TypeError: first argument must be an iterable of pandas objects, you passed an object of type “DataF
使用concat()函数拼接两个表格,出现以下错误: TypeError: first argument must be an iterable of pandas objects, you pass ...
最新文章
- MOOS学习笔记1——HelloWorld
- 黑客勒索遭遇霸气回应:不怕,我们有备份
- Rdlc报表出现空白页解决方法
- dependencyManagement使用简介
- 学材料化学想转行计算机,2020大学最没用的十大专业-十大劝退专业(转专业必看)...
- 步进电机基础(5.9)-步进电机的驱动与控制-三相步进电机的驱动电路
- SQl触发器 声明变量。
- 服务器搬迁方案_机房搬迁方案
- 这4种领导能力,别等变革失败了才知道!
- 关于Python列表解析式以及初始化指定size列表
- 机械转计算机,成功上岸鹅厂。白菜价年薪40w
- python storm连接mysql_Python ORM Storm 源码修改
- 朋友圈发圈助手文案,头像,壁纸组合微信小程序源码下载
- EasyClick 蓝奏云+石墨文档实现热更新
- 树莓派 安装Transmission并自动挂载移动硬盘
- 【Rosalind】Counting Point Mutations
- nginx NSS error -12263 (SSL_ERROR_RX_RECORD_TOO_LONG) 处理
- 欢迎前来日本王子酒店享用当地应季美食
- 后置定语用法归纳-A summary for usage of postpostional atrributes
- 安装SVN服务端软件VisualSVN
热门文章
- Windows + IDEA + SBT 打造Spark源码阅读环境
- Windows平台下sbt的安装设置
- ARGMAX(以及ARGMIN)的意思
- java ftp connect_java操作Ftp文件的一些方式(一)
- c# mysql 时间_c# – 无法在VS2010中将MySQL日期/时间值转换为System.DateTime
- java反编译软件_安卓强大的逆向软件,媲美MT管理器!
- dom4j工具类_基于DOM4J的XML文件解析类
- pandas keyerror: 标签_Hinton新作!越大的自监督模型,半监督学习需要的标签越少...
- php比较两个数组的键名统计有交集的数量,php比较两个数组的键名并返回交集的函数array_intersect_key()...
- mysql数据库安全配置规范_MySQL数据库安全配置