我有2个模板。
-Main layout
-create_device(template 1)
-add_device(template 2)
-add_device(template 2)
-add_device(template 2)
所以我有create_device
布局作为模板,然后是add_device
模板。我想要做的是每当用户点击“添加设备”时,我想附加add_device
模板。
每个白色矩形都是add_device
模板。我试着用块
但是当我点击“添加设备”时,它每次都会覆盖我的块并且不会追加。它还会删除我的标题:
我尝试在add_device
模板
create_device:
{% block modbus-device %}
{% endblock %}
add_device:
{% extends "app/create_modbus.html" %}
{% block modbus-device %}
<div class="panel panel-default" id="dev_{{ total_forms }}">
<div class="panel-body">
.....
</div>
</div>
编辑,我需要附加模板的原因是因为每个模板包含1个表单和1个formset,我可以轻松地从我的视图生成,而不是使用jquery来解析和更改id和name属性
形式:
class ModbusRegistersForm(ModelForm):
ixIOType = ModelChoiceField(queryset=IOType.objects.all())
ixIOType.widget = Select(attrs={'class': 'form-control', 'data-form': '0'})
bRange = BooleanField(required=False)
bRange.widget.attrs['data-form'] = 0
class Meta:
model = Register
fields = ['sRegisterName','iStartingAddr','bRange','ixIOType','iOffset']
widgets = {
'sRegisterName': TextInput(attrs={'placeholder': 'Register Name','class': 'form-control', 'data-form': '0'}),
'iStartingAddr': TextInput(attrs={'placeholder': 'Starting address','class': 'form-control','data-form': '0'}),
'iOffset': TextInput(attrs={'placeholder': 'Address offset','class': 'form-control','data-form': '0'}),
}
class CreateModbusForm(ModelForm):
class Meta:
model = ModbusDevice
fields = ['ixModbusDevice', 'sModbusName','iPort', 'iSlave', 'sIP']
widgets = {
'sModbusName': TextInput(attrs={'placeholder': 'Device Name','class': 'form-control','data-form': '0',}),
'iPort': TextInput(attrs={'placeholder': 'Port','class': 'form-control','data-form': '0',}),
'iSlave': TextInput(attrs={'placeholder': 'Slave id','class': 'form-control','data-form': '0',}),
'sIP': TextInput(attrs={'placeholder': 'Modbus IP address','class': 'form-control','data-form': '0',})
}
型号:
class ModbusDevice(models.Model):
ixModbusDevice = models.AutoField(primary_key=True)
sModbusName = models.CharField(verbose_name='Device Name',max_length=100)
iPort = models.IntegerField(verbose_name='Port')
iSlave = models.IntegerField(verbose_name='Slave ID')
sIP = models.GenericIPAddressField(verbose_name='IP Address')
class Meta:
db_table = 'TModbusDevice'
#Modbus tables
class Register(models.Model):
ixRegister = models.AutoField(primary_key=True)
sRegisterName = models.CharField(max_length=100)
iStartingAddr = models.IntegerField()
bRange = models.BooleanField(default=False)
ixIOType = models.ForeignKey(IOType)
ixModbusDevice = models.ForeignKey(ModbusDevice)
iOffset = models.IntegerField(blank=True, null=True)
class Meta:
db_table = 'TRegister'
视图:
devices = []
modbus_qset = ModbusDevice.objects.all()
if modbus_qset:
for index, device in enumerate(modbus_qset):
container = ModbusContainer()
modbus_form = CreateModbusForm(instance=device, prefix="modbus-id_" + str(device.pk))
container.modbus_device = modbus_form
register_qset = Register.objects.filter(ixModbusDevice=device)
if register_qset:
InlineFormset = inlineformset_factory(ModbusDevice, Register, ModbusRegistersForm, extra=0)
register_forms = InlineFormset(instance=device, queryset=register_qset, prefix="register-id_" + str(device.pk))
container.registers = register_forms
else:
InlineFormset = inlineformset_factory(ModbusDevice, Register, ModbusRegistersForm, extra=1)
register_forms = InlineFormset(prefix="register-id_" + str(device.pk))
container.registers = register_forms
total_forms = index+1
devices.append(container)
答案 0 :(得分:0)
在django那里formset
,这绝对是你想要的。 formset的作用是动态创建/更改/删除项目列表。因此,您应该检查原始文档:https://docs.djangoproject.com/en/1.8/topics/forms/formsets/,而不是进行模板继承。在这里给你一个具体的例子很难,但谷歌搜索django formset会给你很多。