如何在 Django 中将小部件添加到 UpdateView
How to add Widgets to UpdateView in Django
我需要将这个小部件添加到 django UpdateView,
class tlistUpdate(LoginRequiredMixin,UpdateView):
fields = ('title', 'thumbnail', 'content', 'tags')
model = htmlpage
template_name = 'blog/create_form.html'
尝试添加
widgets = {
'content': SummernoteWidget(),
}
和
content = forms.CharField(widget=SummernoteWidget())
但是没有用。
UpdateView
未构造为处理高级表单构造。这个想法是,如果您打算构建一个 简单 (标准)Form
.
,则使用 fields
您可以简单地构建一个 ModelForm
并在您的 CreateView
/UpdateView
中使用该表格:
# app/forms.py
from django import forms
class HtmlPageForm(forms.ModelForm):
class Meta:
model = HtmlPage
fields = ('title', 'thumbnail', 'content', 'tags')
<b>widgets = {</b>
'content': SummernoteWidget
<b>}</b>
在您的 views.py
中,您可以通过设置 form_class
attribute [Django-doc]:
来使用表格
# app/views.py
from app.forms import HtmlPageForm
class TlistUpdate(LoginRequiredMixin,UpdateView):
model = htmlpage
<b>form_class = HtmlPageForm</b>
template_name = 'blog/create_form.html'
Note: normally a Django models, just like all classes in Python are given a name in PerlCase, not snake_case, so it should be: HtmlPage
instead of htmlpage
.
我需要将这个小部件添加到 django UpdateView,
class tlistUpdate(LoginRequiredMixin,UpdateView):
fields = ('title', 'thumbnail', 'content', 'tags')
model = htmlpage
template_name = 'blog/create_form.html'
尝试添加
widgets = {
'content': SummernoteWidget(),
}
和
content = forms.CharField(widget=SummernoteWidget())
但是没有用。
UpdateView
未构造为处理高级表单构造。这个想法是,如果您打算构建一个 简单 (标准)Form
.
fields
您可以简单地构建一个 ModelForm
并在您的 CreateView
/UpdateView
中使用该表格:
# app/forms.py
from django import forms
class HtmlPageForm(forms.ModelForm):
class Meta:
model = HtmlPage
fields = ('title', 'thumbnail', 'content', 'tags')
<b>widgets = {</b>
'content': SummernoteWidget
<b>}</b>
在您的 views.py
中,您可以通过设置 form_class
attribute [Django-doc]:
# app/views.py
from app.forms import HtmlPageForm
class TlistUpdate(LoginRequiredMixin,UpdateView):
model = htmlpage
<b>form_class = HtmlPageForm</b>
template_name = 'blog/create_form.html'
Note: normally a Django models, just like all classes in Python are given a name in PerlCase, not snake_case, so it should be:
HtmlPage
instead of.htmlpage