【发布时间】:2017-07-22 11:24:37
【问题描述】:
我正在使用django-crispy-forms 生成一个简单的表单,该表单显示在 jquery 对话框中。我的模型如下:
class DummyModel(models.Model):
name = models.CharField(max_length=100)
description = models.TextField(max_length=150)
time_points = models.PositiveIntegerField()
more_text = models.CharField(max_length=100)
class Meta:
db_table = "dummy"
我在form 类中使用FormHelper 对象来自定义一些lookout,如以下脆皮表单文档中所示:
class DummyForm(ModelForm):
def __init__(self, *args, **kwargs):
super(DummyForm, self).__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.form_class = 'form-horizontal'
self.helper.layout = Layout(
Field('name', css_class='input-xlarge'),
Field('description', rows="3", css_class='input-xlarge'),
)
class Meta:
model = DummyModel
fields = ['name', 'description']
现在在我的模板中,我执行以下操作:
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block title %}Dummy | Test {% endblock %}
{% block content %}
<div id="dialog" title="Edit" style="display: none;">
<form method="post" action="">
{% csrf_token %}
{% crispy DummyForm %}
<input type="submit" value="Submit Form"/>
</form>
</div>
{% load static %}
{% load render_table from django_tables2 %}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
function EditDialog() {
$( "#dialog" ).dialog();
return false;
}
</script>
<div class="function-page">
<div class="table-form">
<div class="function-container">
{% render_table reviews %}
</div>
</div>
</div>
{% endblock %}
当从链接调用EditDialog() javascript 时会加载表单,这会创建一个包含表单的 jquery 对话框。
但是,我的格式似乎没有任何效果。例如,在此处的示例要点 (https://github.com/django-crispy-forms/django-crispy-forms) 中,标签和组件是水平布局的,但对我来说它们是垂直布局的。我还看到表单中的标签旁边有一个*,这很奇怪。随附的屏幕截图显示了它。
【问题讨论】:
标签: django django-forms django-crispy-forms