python - 使用 Django 从另一个 CBV 获取上下文
问题描述
我想获得您的帮助,以便通过get_context_data()
.
客观的:
基本上,我有一个ListView()
带有pagination
. 我拿起当前页面在get_context_data()
. 然后,我想编辑一个对象,所以我要去UpdateView()
.
当我发布编辑时,它应该返回到以前的分页。例如,如果对象在第 32 页,在 之后POST request
,我需要被重定向到第 32 页。
我的代码:
class DocumentListView(AdminMixin, CustomListSearchView):
""" Render the list of document """
def get_context_data(self, **kwargs):
context = super(DocumentListView, self).get_context_data(**kwargs)
actual_page = self.request.GET.get('page', 1)
if actual_page:
context['actual_page'] = actual_page
return context
class DocumentFormView(CustomPermissionRequiredMixin, DocumentListView, UpdateView):
def get_current_page(self, **kwargs):
context = super(DocumentListView, self).get_context_data(**kwargs)
actual_page = context['actual_page']
return actual_page
def get_context_data(self, **kwargs):
context = super(DocumentFormView, self).get_context_data(**kwargs)
print(self.get_current_page())
...
return context
def post(self, request, pk=None):
...
return render(request, 'app/manage_document_form.html', context)
问题:
我没有克服actual_page
在我的班上DocumentFormView
。
我遇到了这个问题:
AttributeError:“DocumentFormView”对象没有属性“对象”
我试图添加我的post()
功能:
self.object = self.get_object()
但这并不能解决问题。
你有什么主意吗 ?
解决方案
推荐阅读
- svg - 将渐变应用于填充路径的 svg 形状
- php - MySQL $row 选择了如何只显示列的前 n 个字符
- visual-studio-code - 如何在 VSCode 中从 Python Interactive 调试 python 模块?
- c++ - 错误:重新定义类,尽管使用了包含守卫
- javascript - 控制台显示意外标识符错误并且代码不运行
- html - 伪元素覆盖主类
- java - 无法在服务中使用 findViewById,使用了 LayoutInflater 但没有帮助,没有显示值
- postgresql - 将 '0' 转换为双精度,然后乘以 -1 将导致负零
- android - Firebase Crashlytics - java.lang.IllegalStateException:缺少 Crashlytics 构建 ID
- swiftui - SwiftUI 中的系统 UIBarButtonItem