- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望能够在我的 django 表单中提取不同的信息:
这是我的表格:
<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
class InstanceForm(ModelForm):
class Meta:
model = models.BaseAsset
widgets = {
'labels': LabelIconCheckboxSelectMultiple()
}
模型:
class AssetClass(models.Model):
default_labels = models.ManyToManyField(Label, null=True, blank=True)
pass
M2M 引用域
class Label(models.Model):
explanation = models.CharField(null=True, max_length=63)
svgpreview = models.CharField(null=True, max_length=31)
def __unicode__(self):
return unicode(self.explanation)
pass
现在,{{ form.as_p }}
生成的 HTML 代码如下:
<li><label for="id_labels_0"><input type="checkbox" name="labels" value="1" id="id_labels_0" /> Consult owner before using</label></li>
<li><label for="id_labels_1"><input type="checkbox" name="labels" value="2" id="id_labels_1" /> This item is broken</label></li>
这意味着它显然使用了 __unicode__
模型“标签”的渲染。我怎样才能在 Select 小部件中更改该行为,以便它使用不同的函数来填充它的选择?我试图以合理便携的方式打印 '<img src="{{label.svgpreview}}" alt="{{label.explanation}}"...>'
在复选框旁边?
最佳答案
您将覆盖 forms.widgets.CheckboxSelectMultiple
类:
这是 CheckboxSelectMultiple 类及其渲染函数:
class CheckboxSelectMultiple(SelectMultiple):
def render(self, name, value, attrs=None, choices=()):
if value is None: value = []
has_id = attrs and 'id' in attrs
final_attrs = self.build_attrs(attrs, name=name)
output = [u'<ul>']
# Normalize to strings
str_values = set([force_unicode(v) for v in value])
for i, (option_value, option_label) in enumerate(chain(self.choices, choices)):
# If an ID attribute was given, add a numeric index as a suffix,
# so that the checkboxes don't all have the same ID attribute.
if has_id:
final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i))
label_for = u' for="%s"' % final_attrs['id']
else:
label_for = ''
cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values)
option_value = force_unicode(option_value)
rendered_cb = cb.render(name, option_value)
option_label = conditional_escape(force_unicode(option_label))
output.append(u'<li><label%s>%s %s</label></li>' % (label_for, rendered_cb, option_label))
output.append(u'</ul>')
return mark_safe(u'\n'.join(output))
那么你会做什么:
class MyCheckboxSelectMultiple(CheckboxSelectMultiple):
def render(self, name, value, attrs=None, choices=()):
#put your code to have custom checkbox control with icon
#...
output.append(u'<li><label%s>%s %s</label></li>' % (label_for, rendered_cb, option_label)) # especially you will be working on this line
#...
那么你使用widgets=CheckboxSelectMultiple()
的地方就会变成widgets=MyCheckboxSelectMultiple()
关于Django CheckboxSelectMultiple 从 ModelForm 覆盖 'choices',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8693251/
我正在使用以下方法初始化表单: MultiSubscriptionForm(initial={'email': user.email}) 在我的表单中,我还想初始化一个 CheckboxSelectM
我正在尝试使用表单的 CheckboxSelectMultiple小部件,但我想改变它的输出方式。有没有办法删除或更改 到别的东西? 最佳答案 implementation of CheckboxSe
我有一个 CheckboxSelectMultiple 字段,为什么我不能遍历单个选项? 这不起作用: {%for choice in form.travels.choices%} {{c
我有一个 ManyToManyField , 显示复选框。当我的 bool 字段 share_data 时应检查复选框等于一。 这是代码: class Participant(models.Model
如何从呈现的选项中删除“------”?我在我的模型表单中使用: widgets = { 'event_form': forms.CheckboxSelectMultiple(), } 在模型
我正在为我的模型创建添加/编辑 View 。我有一个多对多的关系,我想将它显示为复选框而不是默认的 SelectMultiple。我做了一些研究,发现我唯一需要做的就是添加类似 objects
有 2 个表域和组具有一对多关系。这些表与用户表有多对多的关系 在用户管理界面上,我将组和域呈现为 CheckboxSelectMultiple小部件。 是否可以以包含 2 列的表格形式呈现此内容:一
希望有人可以帮助我对此有所了解。我正在开发一个通知应用程序,当针对特定类别 IE 提交博客文章时,用户可以注册以接收电子邮件,例如,我只想要猫的媒体和购买的电子邮件 我有一个基本模型定义为 模型 cl
我陷入了“CheckboxSelectMultiple”,我不知道如何提取用户选择的内容和未选择的内容。 例如,我的forms.py: from django import forms FAVORIT
@register_snippet class Numbers(models.Model): number = models.IntegerField() class State(models
我有这两个模型: 模型.py class App(models.Model): app_name = models.SlugField(max_length=50) options_l
表单.py class FormEntry(forms.ModelForm): class Meta: model = Entry fields = ['name','price'
我有一个 django 表单,允许用户选择多个选项: CARDS = ( ("visa", "Visa"), ("mastercard", "MasterCard"), ) class
我正在构建表单的“编辑”页面。此页面应该显示包含已保存数据的表单,因此该表单已预先填充了已保存的数据。对于大多数字段来说,它工作得很好,但我遇到了无法恢复的 MultipleChoiceField/C
我希望能够在我的 django 表单中提取不同的信息: 这是我的表格: {% csrf_token %} {{ form.as_p }} class InstanceForm(ModelForm)
我对 Django 还很陌生,所以请耐心等待。 我正在使用 ModelForm 和 Django-Material(由 Viewflow 提供),它的工作效果非常好,值得一提。它不显示复选框。 我的“
我想要类似管理界面的东西。 表单代码如下: class NewRoleFrom(forms.Form): role = forms.ModelMultipleChoiceField(
在我的 Django 应用程序中,我有以下模型: class SuperCategory(models.Model): name = models.CharField(max_length=100
我有 Django ModelForm对于具有 ManyToManyField 的模型。我想将此字段的小部件更改为 CheckboxSelectMultiple .我可以在不覆盖表单定义中的字段的情况
我有一个表格 class TypesForm(forms.Form): .... types = forms.ModelChoiceField( label='Types', q
我是一名优秀的程序员,十分优秀!