在视图中操作脆皮形式的助手

Manipulating a helper of a crispy form in a view

刚开始在 Django 中使用 crispy 表单,到目前为止它很棒!然而,我遇到了以下问题:

每当我尝试在我的视图中操作我的助手时,如此处解释:https://django-crispy-forms.readthedocs.org/en/d-0/tags.html,我得到以下类型错误:'ContactForm' 对象不可调用。

forms.py

  1 from django import forms                                                        
  2 from django.core.urlresolvers import reverse, reverse_lazy                      
  3 from .models import Contact                                                     
  4 from django.utils.translation import ugettext_lazy as _                         
  5                                                                                 
  6 from crispy_forms.helper import FormHelper                                      
  7 from crispy_forms.layout import Submit                                          
  8                                                                                 
  9                                                                                 
 10 class ContactForm(forms.ModelForm):                                             
 11                                                                                 
 12     def __init__(self, *args, **kwargs):                                        
 13         self.helper = FormHelper()                                              
 14         self.helper.add_input(Submit('add_contact', 'Add contact'))
 15         self.helper.show_errors = True                                          
 16         self.helper.form_action = reverse_lazy('contacts:create_contact')                                      
 17         super(ContactForm, self).__init__(*args, **kwargs)                      
 18                                                                                 
 19     class Meta:                                                                  
 20         model = Contact                                                         
 21         fields = ['title']      

views.py

  2 from __future__ import absolute_import, unicode_literals                        
  3                                                                                 
  4 from django.core.urlresolvers import reverse, reverse_lazy                      
  5 from django.views.generic import DetailView, ListView, RedirectView, UpdateView, FormView
  6 from django.views.generic.edit import CreateView                                
  7 from django.contrib.auth.decorators import login_required                       
  8 from django.shortcuts import render                                             
  9                                                                                 
 10 from braces.views import LoginRequiredMixin                                     
 11 from .forms import ContactForm                                                  
 12 from .models import Contact                                                                                  
 13                                                                                                              
 14                                                                                                                                                                                                                          
 20 class CreateContactView(LoginRequiredMixin, CreateView):                                                     
 21     template_name = 'contacts/contact_form.html'                                
 22     form_class = ContactForm()  
 23     form_class.helper.form_action = ""                                                                              
 23     model = Contact                                                             
 24     success_url = reverse_lazy('contacts:list_contacts')

你们知道我做错了什么吗?如何使此表单可调用?显然错误是从我的视图的第 22 行开始的,我可以通过删除第 23 行并删除第 22 行的括号来解决它,但是如果我想更改 helper.form_action,我必须这样做。

问题出在您 CreateContactViewform_class 定义中。使用基于 Class 的视图时,您不必实例化它(即 ContactForm())。您需要将其更改为:

class CreateContactView(LoginRequiredMixin, CreateView):                                                     
    template_name = 'contacts/contact_form.html'                                
    form_class = ContactForm 
    form_class.helper.form_action = ""                                                                              
    model = Contact                                                             
    success_url = reverse_lazy('contacts:list_contacts')

当您在基于 class 的视图中设置 form_class 时,您必须使用 class,而不是 它的实例

class CreateContactView(LoginRequiredMixin, CreateView):                                                     
    template_name = 'contacts/contact_form.html'                                
    form_class = ContactForm

此代码在服务器启动时加载一次。

如果要更改助手,则需要在视图的方法中编辑表单实例。此代码是 运行 每个请求一次。例如,您可以覆盖 get_form.

class CreateContactView(LoginRequiredMixin, CreateView):                                                     
    template_name = 'contacts/contact_form.html'                                
    form_class = ContactForm

    def get_form(self, form_class=None):
        form = super(CreateContactView, self).get_form(form_class)
        form.helper.form_action = ""
        return form                                                                            

但是,对于您给出的示例,在表单的 __init__ 方法中设置操作会更容易。那么您也不必重写 get_form 方法。

 class ContactForm(forms.ModelForm):                                             

    def __init__(self, *args, **kwargs):                                        
        self.helper = FormHelper()
        self.helper.form_action = ""                                                                              
        ...