在使用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相关推荐

  1. 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 ...

  2. Python+selenium自动化测试:报错:TypeError: 'WebElement' object is not iterable

    代码: # -*- coding: utf-8 -*-from selenium import webdriverfrom selenium.webdriver.common.by import By ...

  3. python nonetype_【已解决】Python程序错误:TypeError: ‘NoneType’ object is not iterable

    [问题] 如下代码:def callBlogFunc(funcToCall, *paraList): funcName = funcToCall.func_name; if(blogIsNetease ...

  4. 成功解决TypeError int object is not iterable

    成功解决TypeError: 'int' object is not iterable 目录 解决问题 解决思路 解决方法 解决问题 TypeError: 'int' object is not it ...

  5. Python报错TypeError: ‘User‘ object is not iterable

    Python报错TypeError: 'User' object is not iterable 原因 User对象是不可迭代的,我这里是因为User并不是list,所以是不可迭代的,所以不使用迭代即 ...

  6. 【Python】处理 TypeError: ‘FirefoxWebElement‘ object is not iterable

    编写下面的代码: from selenium import webdriverdriver = webdriver.Firefox() driver.maximize_window() driver. ...

  7. TypeError: 'float' object is not iterable

    求心率的peason相关系数的时候遇到这个问题: TypeError: 'float' object is not iterable 解决方法: pre_label.extend(output) ty ...

  8. python常见错误 TypeError: 'NoneType' object is not iterable

    TypeError: 'NoneType' object is not iterable 1. 将None赋给多个值时,会出现提示:TypeError: 'NoneType' object is no ...

  9. 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 ...

最新文章

  1. MOOS学习笔记1——HelloWorld
  2. 黑客勒索遭遇霸气回应:不怕,我们有备份
  3. Rdlc报表出现空白页解决方法
  4. dependencyManagement使用简介
  5. 学材料化学想转行计算机,2020大学最没用的十大专业-十大劝退专业(转专业必看)...
  6. 步进电机基础(5.9)-步进电机的驱动与控制-三相步进电机的驱动电路
  7. SQl触发器 声明变量。
  8. 服务器搬迁方案_机房搬迁方案
  9. 这4种领导能力,别等变革失败了才知道!
  10. 关于Python列表解析式以及初始化指定size列表
  11. 机械转计算机,成功上岸鹅厂。白菜价年薪40w
  12. python storm连接mysql_Python ORM Storm 源码修改
  13. 朋友圈发圈助手文案,头像,壁纸组合微信小程序源码下载
  14. EasyClick 蓝奏云+石墨文档实现热更新
  15. 树莓派 安装Transmission并自动挂载移动硬盘
  16. 【Rosalind】Counting Point Mutations
  17. nginx NSS error -12263 (SSL_ERROR_RX_RECORD_TOO_LONG) 处理
  18. 欢迎前来日本王子酒店享用当地应季美食
  19. 后置定语用法归纳-A summary for usage of postpostional atrributes
  20. 安装SVN服务端软件VisualSVN

热门文章

  1. Windows + IDEA + SBT 打造Spark源码阅读环境
  2. Windows平台下sbt的安装设置
  3. ARGMAX(以及ARGMIN)的意思
  4. java ftp connect_java操作Ftp文件的一些方式(一)
  5. c# mysql 时间_c# – 无法在VS2010中将MySQL日期/时间值转换为System.DateTime
  6. java反编译软件_安卓强大的逆向软件,媲美MT管理器!
  7. dom4j工具类_基于DOM4J的XML文件解析类
  8. pandas keyerror: 标签_Hinton新作!越大的自监督模型,半监督学习需要的标签越少...
  9. php比较两个数组的键名统计有交集的数量,php比较两个数组的键名并返回交集的函数array_intersect_key()...
  10. mysql数据库安全配置规范_MySQL数据库安全配置