django视图无法配置筛选器
每次使用django视图无法配置筛选器,django,django-models,django-templates,django-views,Django,Django Models,Django Templates,Django Views,每次使用author=request.user,我都会收到一个错误,错误如下: namererror位于/ 未定义名称“请求” 请求方法:获取 请求URL:http://127.0.0.1:8000/ Django版本:2.0.2 异常类型:NameError 异常值: 未定义名称“请求” 异常位置:get_context_数据第24行中的C:\Django\Django\MyProject\Pitsnews\views.py Python可执行文件:C:\Users\deepd\AppData
author=request.user
,我都会收到一个错误,错误如下:
namererror位于/
未定义名称“请求”
请求方法:获取
请求URL:http://127.0.0.1:8000/
Django版本:2.0.2
异常类型:NameError
异常值:
未定义名称“请求”
异常位置:get_context_数据第24行中的C:\Django\Django\MyProject\Pitsnews\views.py
Python可执行文件:C:\Users\deepd\AppData\Local\Programs\Python\Python36\Python.exe
Python版本:3.6.1
Python路径:
['C:\\Django\\Django\\MyProject',
'C:\\Users\\deepd\\AppData\\Local\\Programs\\Python\\Python36\\Python36.zip',
'C:\\Users\\deepd\\AppData\\Local\\Programs\\Python\\Python36\\DLLs',
'C:\\Users\\deepd\\AppData\\Local\\Programs\\Python\\Python36\\lib',
'C:\\Users\\deepd\\AppData\\Local\\Programs\\Python\\Python36',
'C:\\Users\\deepd\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site packages',
'C:\\Users\\deepd\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site packages\\django-2.0.2-py3.6.egg',
'C:\\Users\\deepd\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site packages\\pytz-2018.3-py3.6.egg']
服务器时间:2018年3月23日星期五18:26:02+0000
回溯切换到复制和粘贴视图
C:\Users\deepd\AppData\Local\Programs\Python36\lib\site packages\django-2.0.2-py3.6.egg\django\core\handlers\exception.py
响应=获取响应(请求)。。。
▶ 局部变量
C:\Users\deepd\AppData\Local\Programs\Python36\lib\site packages\django-2.0.2-py3.6.egg\django\core\handlers\base.py in \u get\u response
响应=自身。通过中间件(e,请求)处理异常。。。
▶ 局部变量
C:\Users\deepd\AppData\Local\Programs\Python36\lib\site packages\django-2.0.2-py3.6.egg\django\core\handlers\base.py in \u get\u response
响应=包装的回调(请求,*回调参数,**回调参数)。。。
▶ 局部变量
C:\Users\deepd\AppData\Local\Programs\Python36\lib\site packages\django-2.0.2-py3.6.egg\django\views\generic\base.py在视图中
返回self.dispatch(请求,*args,**kwargs)。。。
▶ 局部变量
C:\Users\deepd\AppData\Local\Programs\Python\36\lib\site packages\django-2.0.2-py3.6.egg\django\contrib\auth\mixins.py在调度中
return super().dispatch(请求,*args,**kwargs)。。。
▶ 局部变量
C:\Users\deepd\AppData\Local\Programs\Python\36\lib\site packages\django-2.0.2-py3.6.egg\django\views\generic\base.py在调度中
返回处理程序(请求、*args、**kwargs)。。。
▶ 局部变量
get中的C:\Users\deepd\AppData\Local\Programs\Python\Python36\lib\site packages\django-2.0.2-py3.6.egg\django\views\generic\list.py
context=self.get\u context\u data()。。。
▶ 局部变量
获取上下文数据中的C:\Django\Django\MyProject\Pitsnews\views.py
上下文['deeppost']=Post.objects.filter(author=request.user)。。。
▶ 局部变量
您直接访问请求
,而不使用CBV-列表视图实例。您需要访问通用视图中的请求对象,如下所示
from django.shortcuts import render
from django.http import HttpResponse
from django.views.generic import TemplateView,ListView,DetailView
from django.contrib.auth.mixins import LoginRequiredMixin
from .models import Post
# Create your views here.
def index(request):
return HttpResponse("Hello, world.")
class HomePageView(LoginRequiredMixin,ListView):
model = Post
template_name = 'home.html'
context_object_name = 'deeppost'
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super().get_context_data(**kwargs)
# Add in a QuerySet of all the books
context['deeppost'] = Post.objects.filter(author=request.user)
return context
您需要使用self.request
self.request.user
使用
self.request.user
而不是request.user
假设我们只想显示标题而不想显示正文,对不起,我不理解django,在我编写deeppost.title和deeppost.body时,在html模板中,标题和正文返回到html,当我在django shell中尝试同样的事情时,它不会,所以我想了解过滤器到底是如何工作的,对于每个应用程序视图,我是否必须创建不同的查询集?。当我发布.objects.all时,它是否应该显示数据库中的每个对象?。如出版日期等
class HomePageView(LoginRequiredMixin,ListView):
model = Post
template_name = 'home.html'
context_object_name = 'deeppost'
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super().get_context_data(**kwargs)
# Add in a QuerySet of all the books
context['deeppost'] = Post.objects.filter(author=self.request.user)
return context