Python 在django中,编写两个相似的ListView或渲染到不同模板中相似列表的最佳方法是什么?
我想在一个页面上向用户显示猫的整个列表,在另一个页面上显示属于该用户的猫的列表。这两个列表几乎相同,只是其中一个由Python 在django中,编写两个相似的ListView或渲染到不同模板中相似列表的最佳方法是什么?,python,django,Python,Django,我想在一个页面上向用户显示猫的整个列表,在另一个页面上显示属于该用户的猫的列表。这两个列表几乎相同,只是其中一个由self.request.user过滤。首先,我编写了两个不同的ListView,但它们的查询集实际上是相同的。我不知道如何从另一个基于类的视图继承一个基于类的视图。可能吗 根据我对CBV继承知识的缺乏,我将这些行放入get\u context\u data: cats_in_agent_base = Cat.objects.filter(cat_owner__exact=user)
self.request.user
过滤。首先,我编写了两个不同的ListView,但它们的查询集实际上是相同的。我不知道如何从另一个基于类的视图继承一个基于类的视图。可能吗
根据我对CBV继承知识的缺乏,我将这些行放入get\u context\u data
:
cats_in_agent_base = Cat.objects.filter(cat_owner__exact=user)
context['cats_in_user_base'] = cats_in_user_base
在个人所有者页面上,django只会参考“用户库中的猫”
,而不会参考猫列表
。但是,当然,用户现在不能通过get请求过滤列表并对其进行分页
解决这个问题的好方法是什么?
如何将queryset
从一个视图继承到另一个视图?例如,如果我想在猫的DetailView
页面上按价格或颜色显示类似猫的列表,该怎么办?我想使用来自CatListView
的相同查询集,并将其过滤到get\u context\u data
方法中
我的CatListView:
class CatListView(LoginRequiredMixin, ListView):
template_name = 'ha/cat_list_template.html'
context_object_name = 'cat_list'
def get_queryset(self):
max_price = self.request.GET.get('max_price')
if max_price is None or max_price == '':
max_price = 1000000000
min_price = self.request.GET.get('min_price')
if min_price is None or min_price == '':
min_price = min(Cat.objects.filter().values_list('cat_price'))[0]
cat_list = Cat.objects.filter(cat_price__gte=min_price) \
.filter(cat_price__lte=max_price).order_by('-cat_pub_date')
paginator = Paginator(cat_list, 20)
page = self.request.GET.get('page')
try:
cat_list = paginator.page(page)
except PageNotAnInteger:
cat_list = paginator.page(1)
except EmptyPage:
cat_list = paginator.page(paginator.num_pages)
return cat_list
def get_context_data(self, **kwargs):
user = self.request.user
context = super(CatListView, self).get_context_data(**kwargs)
cats_in_agent_base = Cat.objects.filter(cat_owner__exact=user)
context['cats_in_user_base'] = cats_in_user_base
return context
我认为你可以用一些方法来实现这一点,我将尝试给出一些基本的方法来实现这一点
我可以在您的get\u context\u data
方法中看到,您总是按用户筛选并向模板发送两个猫列表,我认为这不是一个好主意
您可以这样做:
将GET
参数添加到url:
http://yourdomain/your-path-to-cat-list/?owned_by_user=1
# 1 it's not a pk is to say True
# Let's use 1 and 0 for this example
# You could use any other value as a flag
# Then you get that querystring in your get_queryset method
def get_queryset(self):
max_price = self.request.GET.get('max_price')
if max_price is None or max_price == '':
max_price = 1000000000
min_price = self.request.GET.get('min_price')
if min_price is None or min_price == '':
min_price = min(Cat.objects.filter().values_list('cat_price'))[0]
cat_list = Cat.objects.filter(cat_price__gte=min_price) \
.filter(cat_price__lte=max_price).order_by('-cat_pub_date')
if int(self.request.GET.get('owned_by_user')):
cat_list = cat_list.filter(cat_owner__exact=self.request.user)
paginator = Paginator(cat_list, 20)
page = self.request.GET.get('page')
try:
cat_list = paginator.page(page)
except PageNotAnInteger:
cat_list = paginator.page(1)
except EmptyPage:
cat_list = paginator.page(paginator.num_pages)
return cat_list
如果要为此创建新视图,但不重复相同的代码,可以从自己的视图进行扩展:
# You have
class CatListView(LoginRequiredMixin, ListView):
# You could use this view for the complete list
...
# You can create a new one
# And modify get_queryset method
class OwnedByUserCatListView(CatListView):
def get_queryset(self):
max_price = self.request.GET.get('max_price')
if max_price is None or max_price == '':
max_price = 1000000000
min_price = self.request.GET.get('min_price')
if min_price is None or min_price == '':
min_price = min(Cat.objects.filter().values_list('cat_price'))[0]
cat_list = Cat.objects.filter(cat_price__gte=min_price) \
.filter(cat_price__lte=max_price).filter(cat_owner__exact=self.request.user).order_by('-cat_pub_date')
paginator = Paginator(cat_list, 20)
page = self.request.GET.get('page')
try:
cat_list = paginator.page(page)
except PageNotAnInteger:
cat_list = paginator.page(1)
except EmptyPage:
cat_list = paginator.page(paginator.num_pages)
return cat_list
我认为你可以用一些方法来实现这一点,我将尝试给出一些基本的方法来实现这一点
我可以在您的get\u context\u data
方法中看到,您总是按用户筛选并向模板发送两个猫列表,我认为这不是一个好主意
您可以这样做:
将GET
参数添加到url:
http://yourdomain/your-path-to-cat-list/?owned_by_user=1
# 1 it's not a pk is to say True
# Let's use 1 and 0 for this example
# You could use any other value as a flag
# Then you get that querystring in your get_queryset method
def get_queryset(self):
max_price = self.request.GET.get('max_price')
if max_price is None or max_price == '':
max_price = 1000000000
min_price = self.request.GET.get('min_price')
if min_price is None or min_price == '':
min_price = min(Cat.objects.filter().values_list('cat_price'))[0]
cat_list = Cat.objects.filter(cat_price__gte=min_price) \
.filter(cat_price__lte=max_price).order_by('-cat_pub_date')
if int(self.request.GET.get('owned_by_user')):
cat_list = cat_list.filter(cat_owner__exact=self.request.user)
paginator = Paginator(cat_list, 20)
page = self.request.GET.get('page')
try:
cat_list = paginator.page(page)
except PageNotAnInteger:
cat_list = paginator.page(1)
except EmptyPage:
cat_list = paginator.page(paginator.num_pages)
return cat_list
如果要为此创建新视图,但不重复相同的代码,可以从自己的视图进行扩展:
# You have
class CatListView(LoginRequiredMixin, ListView):
# You could use this view for the complete list
...
# You can create a new one
# And modify get_queryset method
class OwnedByUserCatListView(CatListView):
def get_queryset(self):
max_price = self.request.GET.get('max_price')
if max_price is None or max_price == '':
max_price = 1000000000
min_price = self.request.GET.get('min_price')
if min_price is None or min_price == '':
min_price = min(Cat.objects.filter().values_list('cat_price'))[0]
cat_list = Cat.objects.filter(cat_price__gte=min_price) \
.filter(cat_price__lte=max_price).filter(cat_owner__exact=self.request.user).order_by('-cat_pub_date')
paginator = Paginator(cat_list, 20)
page = self.request.GET.get('page')
try:
cat_list = paginator.page(page)
except PageNotAnInteger:
cat_list = paginator.page(1)
except EmptyPage:
cat_list = paginator.page(paginator.num_pages)
return cat_list