Django填充表格

时间:2017-11-07 08:58:41

标签: django django-templates

在我的admin.py中的modelClass中,我定义了子类“changelist_view”和“changeform_view”,以便将两个自定义值发送到我的模板视图。

结果当我打开我的模型表单的更改页面时,它仍然是空的。我可以填写表格吗?

class WorkerAdmin(admin.ModelAdmin):
  change_form_template = 'admin/change_form.html'
  change_list_template = 'admin/change_list.html'

  form = WorkerAdminForm

  def changelist_view(self, request, extra_context=None):
    extra_context = extra_context or {}
    extra_context['model_title'] = 'Workers'
    extra_context['page_title']  = 'Workers Manage'
    return super(WorkerAdmin, self).changelist_view(request, extra_context=extra_context)

  def changeform_view(self, request, object_id=None, form_url='', extra_context=None):
    extra_context = extra_context or {}
    extra_context['model_title'] = 'Workers'
    extra_context['page_title']  = 'Worker Add'
    return super(WorkerAdmin, self).changeform_view(request, extra_context=extra_context)

admin.site.register(Worker,WorkerAdmin)

1 个答案:

答案 0 :(得分:1)

您错过了将object_id和form_url参数传递给超类。

return super(WorkerAdmin, self).changeform_view(request, object_id=object_id, form_url=form_url, extra_context=extra_context)