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
每次我使用author=request.user
时,都会收到错误消息:
NameError at /
name 'request' is not defined
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 2.0.2
Exception Type: NameError
Exception Value:
name 'request' is not defined
Exception Location: C:\Django\Django\MyProject\Pitsnews\views.py in get_context_data, line 24
Python Executable: C:\Users\deepd\AppData\Local\Programs\Python\Python36\python.exe
Python Version: 3.6.1
Python Path:
['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']
Server time: Fri, 23 Mar 2018 18:26:02 +0000
Traceback Switch to copy-and-paste view
C:\Users\deepd\AppData\Local\Programs\Python\Python36\lib\site-packages\django-2.0.2-py3.6.egg\django\core\handlers\exception.py in inner
response = get_response(request) ...
▶ Local vars
C:\Users\deepd\AppData\Local\Programs\Python\Python36\lib\site-packages\django-2.0.2-py3.6.egg\django\core\handlers\base.py in _get_response
response = self.process_exception_by_middleware(e, request) ...
▶ Local vars
C:\Users\deepd\AppData\Local\Programs\Python\Python36\lib\site-packages\django-2.0.2-py3.6.egg\django\core\handlers\base.py in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
C:\Users\deepd\AppData\Local\Programs\Python\Python36\lib\site-packages\django-2.0.2-py3.6.egg\django\views\generic\base.py in view
return self.dispatch(request, *args, **kwargs) ...
▶ Local vars
C:\Users\deepd\AppData\Local\Programs\Python\Python36\lib\site-packages\django-2.0.2-py3.6.egg\django\contrib\auth\mixins.py in dispatch
return super().dispatch(request, *args, **kwargs) ...
▶ Local vars
C:\Users\deepd\AppData\Local\Programs\Python\Python36\lib\site-packages\django-2.0.2-py3.6.egg\django\views\generic\base.py in dispatch
return handler(request, *args, **kwargs) ...
▶ Local vars
C:\Users\deepd\AppData\Local\Programs\Python\Python36\lib\site-packages\django-2.0.2-py3.6.egg\django\views\generic\list.py in get
context = self.get_context_data() ...
▶ Local vars
C:\Django\Django\MyProject\Pitsnews\views.py in get_context_data
context['deeppost'] = Post.objects.filter(author=request.user) ...
▶ Local vars
答案 0 :(得分:0)
您在不使用CBV - 列表视图实例的情况下直接访问request
。您需要访问通用视图中的请求对象,如下所示
self.request.user
答案 1 :(得分:0)
您需要使用self.request
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