我正在尝试在Django和Crispy表单中执行html5表单,但我得到TypeError: __init__() got an unexpected keyword argument 'instance'
:
forms.py
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
from django import forms
class ShareImageForm(forms.Form):
def __init__(self, *args, **kwargs):
super(ShareImageForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_id = 'id-exampleForm'
self.helper.form_class = 'blueForms'
self.helper.form_method = 'post'
self.helper.form_action = 'submit_survey'
self.helper.add_input(Submit('submit', 'Submit'))
data_center = forms.TypedChoiceField(
label = "Please pick a region: ",
choices = ((1, "IAD"), (2, "ORD"),
(3, "DFW"), (4, "HKG")),
widget = forms.RadioSelect,
initial = '1',
required = True,
)
views.py
from django.views.generic import View, CreateView, TemplateView, ListView, DetailView, DeleteView, UpdateView
from django.http import Http404, HttpResponse
from django.conf import settings
from .forms import ShareImageForm
class ShareImage(CreateView, ShareImageForm):
template_name ="share_image.html"
form_class = ShareImageForm
success_url = '/'
def form_valid(self, form):
return super(CreateRequest, self).form_valid(form)
答案 0 :(得分:3)
ShareImageForm
应继承自ModelForm
。
参考:Getting __init__() got an unexpected keyword argument 'instance' with CreateView of Django