Python Django:如何在一个模板中使用两个以上基于类的泛型视图
我试图创建一个web应用程序,用户可以在更新帖子时查看其他帖子。因此,我想在同一个模板中同时使用ListView和UpdateView My Views.py:Python Django:如何在一个模板中使用两个以上基于类的泛型视图,python,django,Python,Django,我试图创建一个web应用程序,用户可以在更新帖子时查看其他帖子。因此,我想在同一个模板中同时使用ListView和UpdateView My Views.py: from django.shortcuts import render from .models import Entry from django.views.generic import ListView from django.views.generic.edit import UpdateView from django.cont
from django.shortcuts import render
from .models import Entry
from django.views.generic import ListView
from django.views.generic.edit import UpdateView
from django.contrib.auth.models import User
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
class index(LoginRequiredMixin, ListView):
template_name = 'diary/index.html'
context_object_name = 'entries'
def get_queryset(self): # def get_queryset(self, request):
return Entry.objects.filter(author=self.request.user)
class EntryUpdate(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
model = Entry
fields = ['title', 'content']
template_name = 'diary/update.html'
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
def test_func(self):
post = self.get_object()
if self.request.user == post.author:
return True
else:
return False
我不知道我是否应该创建另一个视图,或者是否有一个内置的功能,所以,如果你们能帮助我,这将是非常有用的
任何帮助都将不胜感激
编辑:
views.py中的我的ListView代码:
class index(LoginRequiredMixin, ListView):
template_name = 'diary/index.html'
context_object_name = 'entries'
def get_queryset(self):
return Entry.objects.filter(author=self.request.user)
class EntryUpdate(LoginRequiredMixin, MultipleObjectMixin,UserPassesTestMixin, UpdateView):
model = Entry
fields = ['title', 'content']
template_name = 'diary/update.html'
def get_queryset(self):
return Entry.objects.filter(author=self.request.user)
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
def test_func(self):
post = self.get_object()
if self.request.user == post.author:
return True
else:
return False
在views.py中查看我的更新视图:
class index(LoginRequiredMixin, ListView):
template_name = 'diary/index.html'
context_object_name = 'entries'
def get_queryset(self):
return Entry.objects.filter(author=self.request.user)
class EntryUpdate(LoginRequiredMixin, MultipleObjectMixin,UserPassesTestMixin, UpdateView):
model = Entry
fields = ['title', 'content']
template_name = 'diary/update.html'
def get_queryset(self):
return Entry.objects.filter(author=self.request.user)
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
def test_func(self):
post = self.get_object()
if self.request.user == post.author:
return True
else:
return False
错误,我得到:
'EntryUpdate' object has no attribute 'object_list'
您可能可以在UpdateView中尝试使用
多对象mixin
您可以使用此mixinget\u queryset()
定义,并访问模板中的object\u list
。查看更多信息
编辑
当然,这里有一个简短的代码示例:
# demo/models.py
from django.db import models
class Title(models.Model):
title = models.CharField(max_length=100)
这就是我在模板中得到的(我在数据库中有10个“标题”,每个标题都带有随机字符):
编辑2
关于您编辑的问题,您缺少视图中“对象列表”的定义,MultipleObjectMixin
需要该定义
请注意,在我的代码示例中,在views.py
中,我使用将填充对象列表的查询定义对象列表。我相信您收到的错误是因为mixin希望收到对象列表
请尝试添加:
# demo/views.py
# omitted imports
class UpdateWithListView(UpdateView, MultipleObjectMixin):
model = Title
template_name_suffix = '_update_form_with_list'
fields = ['title']
object_list = Title.objects.all() # make sure to define this with your query
update_with_list_view = UpdateWithListView.as_view()
如果我没有弄错的话,get\u queryset()
方法负责UpdateView
的对象检索,而object\u list
与ListView
相关
请尝试将object\u列表添加到您的视图中,并检查它是否解决了问题。如果您能在views.py中帮助我做一件事,那将非常有用,在您执行[object\u list=Title.objects.all()]的地方,我实际上想过滤用户的帖子,所以在ListView中,我用这个代码过滤用户的帖子def get_queryset(self):return Entry.objects.filter(author=self.request.user)
当然,如果我知道,我会分享它:)但是现在当我做同样的事情时,Django给我一个错误“EntryUpdate”对象没有属性“object_list”
我很乐意看到整个视图代码,你可以编辑你的OP来包含代码吗?很抱歉我没有及时回复你,但是现在我已经更新了这个问题,如果你想看到我的代码,请告诉我,我真的很感谢你的帮助!
# demo/views.py
# omitted imports
class UpdateWithListView(UpdateView, MultipleObjectMixin):
model = Title
template_name_suffix = '_update_form_with_list'
fields = ['title']
object_list = Title.objects.all() # make sure to define this with your query
update_with_list_view = UpdateWithListView.as_view()