无法恢复形式

时间:2016-04-06 14:42:49

标签: django django-forms django-templates django-admin django-views

我想从add_view为Django Admin中的特定模板调用默认表单。我将使用它的值(我通过表单从模板中恢复)并完成我的工作。我怎么称呼它?

def add_view(self, request, form_url='', extra_context=None):
    if request.method == 'POST':
    form = defaultForm(request.POST)
    if form.is_valid():
        field1= formulario.cleaned.data['field1'] + "\n" 
        #DO MY STUFF
    else:
        raise raise ValidationError(_("Error1"), code='Error1')

return HttpResponseRedirect("/admin/db/table_name/")

1 个答案:

答案 0 :(得分:1)

ModelAdmin有' get_form'方法:

...
        default_form = self.get_form(request)
        form = default_form(request.POST, request.FILES, instance=None)
...

看看文档: https://docs.djangoproject.com/en/1.9/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_form