在同一模板页面上显示分页的 ListView 和 UpdateView
Show a paginated ListView and an UpdateView on the same template page
我正在尝试创建一个 Django
页面,其中可以更新某些内容并可以在分页 table 中查看某些内容。该模型如下所示:
class CostGroup(models.Model):
name = models.CharField(max_length=200)
description = models.CharField(max_length=200)
def get_absolute_url(self):
return reverse(
'costgroup_detail',
kwargs={
'costgroup_pk': self.pk,
}
)
class Cost(models.Model):
cost_group = models.ForeignKey(CostGroup)
amount = models.DecimalField(max_digits=50, decimal_places=2)
def get_absolute_url(self):
return reverse(
'cost_detail',
kwargs={
'cost_pk': self.pk,
}
)
因此,编辑表单适用于 CostGroup
模型的 name
和 description
字段,table 应显示“金额”列表
我以前只通过表单的 UpdateView
和表单模板中包含的 table 来工作。但是现在,因为我想在 table 上包含分页,所以我需要在同一页面上使用两个视图。我设计的页面最终应该是这样的:
目前我并不担心样式问题,我目前的主要关注点是让表单和 table 位于同一页面上。在目前的状态下,我唯一没有的是 table:
的分页
当前视图如下所示:
class CostDetail(UpdateView):
model = models.Cost
pk_url_kwarg = 'cost_pk'
template_name = 'main/cost_detail.html'
form_class = forms.CostDetailEditForm
success_url = reverse_lazy('cost_list')
我觉得利用 Django CBV 使用的底层混合可能是可行的方法,但我不确定如何开始。
如有任何帮助,我们将不胜感激
感谢您的宝贵时间
我现在正在开发的一个应用程序使用了类似的方法。我从 ListView 开始,引入 FormMixin,然后从 FormView 引入 post()。
class LinkListView(FormMixin, ListView):
model = Link
ordering = ['-created_on']
paginate_by = 10
template_name = 'links/link_list.html'
form_class = OtherUserInputForm
#=============================================================================#
#
# Handle form input
#
def post(self, request, *args, **kwargs):
"""
Handles POST requests, instantiating a form instance with the passed
POST variables and then checked for validity.
"""
form = self.get_form()
self.form = form
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
def put(self, *args, **kwargs):
return self.post(*args, **kwargs)
def get_success_url(self):
return reverse('links')
您可能还希望覆盖 get_object()
、get_queryset()
和 get_context()
。
(这个澄清作为新答案似乎更有效)
看起来您正在处理两个表。对象级别使用成本组,而列表视图显示成本中链接到成本组的子记录。假设这是真的,下面是我将如何进行:
class CostDetail(ModelFormMixin, ListView):
model = CostGroup # Using the model of the record to be updated
form_class = YourFormName # If this isn't declared, get_form_class() will
# generate a model form
ordering = ['id']
paginate_by = 10
template_name = 'main/cost_detail.html' # Must be declared
def get_queryset(self):
# Set the queryset to use the Cost objects that match the selected CostGroup
self.queryset = Cost.objects.filter(cost_group = get_object())
# Use super to add the ordering needed for pagination
return super(CostDetail,self).get_queryset()
# We want to override get_object to avoid using the redefined get_queryset above
def get_object(self,queryset=None):
queryset = CostGroup.objects.all()
return super(CostDetail,self).get_object(queryset))
# Include the setting of self.object in get()
def get(self, request, *args, **kwargs):
# from BaseUpdateView
self.object = self.get_object()
return super(CostDetail,self).get(request, *args, **kwargs)
# Include the contexts from both
def get_context_data(self, **kwargs):
context = ModelFormMixin.get_context_data(**kwargs)
context = ListView.get_context_data(**context)
return context
# This is the post method found in the Update View
def post(self, request, *args, **kwargs):
# From BaseUpdateView
self.object = self.get_object()
# From ProcessFormView
form = self.get_form()
self.form = form
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
def put(self, *args, **kwargs):
return self.post(*args, **kwargs)
我还没有尝试过 运行 这个,所以可能会有错误。祝你好运!
(记住 ccbv.co.uk 是你挖掘基于 Class 的视图时的朋友)
我正在尝试创建一个 Django
页面,其中可以更新某些内容并可以在分页 table 中查看某些内容。该模型如下所示:
class CostGroup(models.Model):
name = models.CharField(max_length=200)
description = models.CharField(max_length=200)
def get_absolute_url(self):
return reverse(
'costgroup_detail',
kwargs={
'costgroup_pk': self.pk,
}
)
class Cost(models.Model):
cost_group = models.ForeignKey(CostGroup)
amount = models.DecimalField(max_digits=50, decimal_places=2)
def get_absolute_url(self):
return reverse(
'cost_detail',
kwargs={
'cost_pk': self.pk,
}
)
因此,编辑表单适用于 CostGroup
模型的 name
和 description
字段,table 应显示“金额”列表
我以前只通过表单的 UpdateView
和表单模板中包含的 table 来工作。但是现在,因为我想在 table 上包含分页,所以我需要在同一页面上使用两个视图。我设计的页面最终应该是这样的:
目前我并不担心样式问题,我目前的主要关注点是让表单和 table 位于同一页面上。在目前的状态下,我唯一没有的是 table:
的分页当前视图如下所示:
class CostDetail(UpdateView):
model = models.Cost
pk_url_kwarg = 'cost_pk'
template_name = 'main/cost_detail.html'
form_class = forms.CostDetailEditForm
success_url = reverse_lazy('cost_list')
我觉得利用 Django CBV 使用的底层混合可能是可行的方法,但我不确定如何开始。
如有任何帮助,我们将不胜感激
感谢您的宝贵时间
我现在正在开发的一个应用程序使用了类似的方法。我从 ListView 开始,引入 FormMixin,然后从 FormView 引入 post()。
class LinkListView(FormMixin, ListView):
model = Link
ordering = ['-created_on']
paginate_by = 10
template_name = 'links/link_list.html'
form_class = OtherUserInputForm
#=============================================================================#
#
# Handle form input
#
def post(self, request, *args, **kwargs):
"""
Handles POST requests, instantiating a form instance with the passed
POST variables and then checked for validity.
"""
form = self.get_form()
self.form = form
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
def put(self, *args, **kwargs):
return self.post(*args, **kwargs)
def get_success_url(self):
return reverse('links')
您可能还希望覆盖 get_object()
、get_queryset()
和 get_context()
。
(这个澄清作为新答案似乎更有效)
看起来您正在处理两个表。对象级别使用成本组,而列表视图显示成本中链接到成本组的子记录。假设这是真的,下面是我将如何进行:
class CostDetail(ModelFormMixin, ListView):
model = CostGroup # Using the model of the record to be updated
form_class = YourFormName # If this isn't declared, get_form_class() will
# generate a model form
ordering = ['id']
paginate_by = 10
template_name = 'main/cost_detail.html' # Must be declared
def get_queryset(self):
# Set the queryset to use the Cost objects that match the selected CostGroup
self.queryset = Cost.objects.filter(cost_group = get_object())
# Use super to add the ordering needed for pagination
return super(CostDetail,self).get_queryset()
# We want to override get_object to avoid using the redefined get_queryset above
def get_object(self,queryset=None):
queryset = CostGroup.objects.all()
return super(CostDetail,self).get_object(queryset))
# Include the setting of self.object in get()
def get(self, request, *args, **kwargs):
# from BaseUpdateView
self.object = self.get_object()
return super(CostDetail,self).get(request, *args, **kwargs)
# Include the contexts from both
def get_context_data(self, **kwargs):
context = ModelFormMixin.get_context_data(**kwargs)
context = ListView.get_context_data(**context)
return context
# This is the post method found in the Update View
def post(self, request, *args, **kwargs):
# From BaseUpdateView
self.object = self.get_object()
# From ProcessFormView
form = self.get_form()
self.form = form
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
def put(self, *args, **kwargs):
return self.post(*args, **kwargs)
我还没有尝试过 运行 这个,所以可能会有错误。祝你好运!
(记住 ccbv.co.uk 是你挖掘基于 Class 的视图时的朋友)