我试图在Calendarium中创建自定义表单/视图,但我一直收到错误
AttributeError: 'module' object has no attribute 'SelectDateWidget'
我的forms.py
中的CreateEventclass CreateEvent(forms.ModelForm):
class Meta:
model = Event
fields = ['title', 'start', 'end', 'description', 'category', 'created_by', 'rule', 'end_recurring_period']
widgets = {
'start': forms.SelectDateWidget()
}
views.py
中的EventCreateViewclass EventCreateView(EventMixin, CreateView):
form_class = CreateEvent
model = Event
有谁知道我为什么会收到此错误
额外(如果允许):
在EventCreateView中传递它" EventMixin"看起来像这样
class EventMixin(object):
"""Mixin to handle event-related functions."""
model = Event
fields = '__all__'
@method_decorator(permission_required('calendarium.add_event'))
def dispatch(self, request, *args, **kwargs):
return super(EventMixin, self).dispatch(request, *args, **kwargs)
有人可以向我解释一下吗?
答案 0 :(得分:2)
您遗失widgets
:
class CreateEvent(forms.ModelForm):
class Meta:
model = Event
fields = ['title', 'start', 'end', 'description', 'category', 'created_by', 'rule', 'end_recurring_period']
widgets = {
'start': forms.widgets.SelectDateWidget()
}
# ^^
另一方面,我怀疑包含该代码段的文件名为forms.py
答案 1 :(得分:1)
根据the docs,您只能从Django 1.9 +中的SelectDateWidget
导入django.forms
小部件。
在早期版本中,您需要从django.forms.extras.widgets
导入它。
首先,添加导入:
from django.forms.extras.widgets import SelectDateWidget
然后将表单中的widgets
更改为:
widgets = {
'start': SelectDateWidget(),
}