我有一个项目正试图重新组合一些字段,其中一些将是可见的,而其他字段将以一种形式显示,因此我可以在字段上应用java脚本进行扩展。任何人都可以建议我如何在我的modelForm类和模板strcuture中做这样的事情?
models.py
class Page(models.Model):
# version control columns
version_from = models.DateTimeField(auto_now_add=True, editable=False)
version_to = models.DateTimeField(null=True, blank=True, editable=False)
# data
name = models.CharField(_('Name'), max_length=10, help_text=_('The name of the page will be used as a display name & in navigation menus'))
show_in_navigation = models.BooleanField(default=True)
slug = models.SlugField(_('Slug'), )
subject = models.CharField(_('Subject'), max_length=30, )
html_title = models.CharField(_('HTML Title'), max_length=15, blank=True, null=True)
keywords = models.CharField(_('HTML Keywords'), max_length=200, blank=True, null=True)
content = models.TextField(_('Content'), )
parent = models.ForeignKey('self', null=True, blank=True)
# audit
created_at = models.DateTimeField(auto_now_add=True)
created_by = models.ForeignKey(User, editable=False)
# objects manager
objects = PageManager()
class Meta:
unique_together = (
'id',
'slug'
)
forms.py
class PageForm(forms.ModelForm):
class Meta:
model = Page
页面form.html
<form class="form well" method="POST">
{% csrf_token %}
{% if form.errors %}
<div class="alert alert-error">
<a class="close" data-dismiss="alert" href="add_blog.html#">×</a>
<h3>Error</h3>
{{ form.non_field_errors }}
{% for field in form %}
{{ field.errors }}
{% endfor %}
</div>
{%endif%}
{% csrf_token %}
{# Include the hidden fields #}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{# Include the visible fields #}
{% for field in form.visible_fields %}
{{ field.label_tag }} {{ field }}
<span class="help-block">{{ field.help_text }}</span>
{% endfor %}
<br />
<input type="submit" value='{% trans 'Login' %}' class="btn" />
</form>
我的挑战是,如何在div标签和最后的html中使字段slug和关键字呈现。
下面是我希望实现的例子
<form class="form well" method="POST">
{% csrf_token %}
{% if form.errors %}
<div class="alert alert-error">
<a class="close" data-dismiss="alert" href="add_blog.html#">×</a>
<h3>Error</h3>
{{ form.non_field_errors }}
{% for field in form %}
{{ field.errors }}
{% endfor %}
</div>
{%endif%}
{% csrf_token %}
{# Include the hidden fields #}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{# Include the visible fields #}
{% for field in form.visible_fields %}
{{ field.label_tag }} {{ field }}
<span class="help-block">{{ field.help_text }}</span>
{% endfor %}
{% for collapsible_field in form.collapsible_fields %}
<div class='collapsible'>
{{ field.label_tag }} {{ field }}
<span class="help-block">{{ field.help_text }}</span>
</div>
{%endfor%}
<br />
<input type="submit" value='{% trans 'Login' %}' class="btn" />
</form>
上面的例子是为了进一步解释我的观点,但我会采取任何其他方法..请指教?
答案 0 :(得分:1)
您提出的理想解决方案看起来不错。 只需在Form类中添加这些:
class PageForm(forms.ModelForm):
class Meta:
model = Page
def normal_fields(self):
return [field for field in self if not field.is_hidden
and field.name in ('field1', 'field2')]
def collapsible_fields(self):
return [field for field in self if not field.is_hidden
and field.name in ('field4', 'field5', 'field3')]
然后在你的模板中使用以下方法:
{# Include the visible fields #}
{% for field in form.normal_fields %}
{{ field.label_tag }} {{ field }}
<span class="help-block">{{ field.help_text }}</span>
{% endfor %}
{% for field in form.collapsible_fields %}
<div class='collapsible'>
{{ field.label_tag }} {{ field }}
<span class="help-block">{{ field.help_text }}</span>
</div>
{%endfor%}