0

I have 2 templates.

-Main layout
   -create_device(template 1)
        -add_device(template 2)
        -add_device(template 2)
        -add_device(template 2)

So I have a create_device layout that is a template and then an add_device template. What I'm trying to do is every time a user clicks on "Add Device" I want to append an add_device template.

What my page looks like:enter image description here

each white rectangle is an add_device template. I tried to use blocks however when I click on "Add Device" it overwrites my block every time and doesn't append. Also it removes my title :enter image description here

I've tried adding {{ block.super }} inside my add_device template

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>

Edit, the reason I need to append a template is because each template contains 1 form and 1 formset which I can easily generate form my view rather than using jquery to parse and changed id's and name attributes

Forms:

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',})
            }

Models:

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'

view:

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)
john
  • 3,949
  • 7
  • 34
  • 56
  • So you want to add a row to the second part (with `Register Name`) each time the user clicks `Add Device`? – knbk Oct 08 '15 at 13:39
  • I want to create a copy of the whole thing. I'll add my forms so you can see how my registers relate to my device – john Oct 08 '15 at 14:05

1 Answers1

0

In django there's formset, which is definitely what you want. What formset does is dynamically create/change/delete a list of items. So instead of doing template inheritance, you should checkout the original documentation: https://docs.djangoproject.com/en/1.8/topics/forms/formsets/. It's hard to give you a concrete example here, but googling django formset would give you tons of those.

Shang Wang
  • 24,909
  • 20
  • 73
  • 94
  • oh I'm aware of formsets however the problem with them was that in this case I would need nested formsets and I couldn't find a away to get them to work. Also that's my bad, the pictures I posted only shows 1 form, on the far right there is a menuy iccon which shows the drop down to the formset attached to my form. Reason is easy device can have multiple registers so im using a formset for every device – john Oct 08 '15 at 13:13
  • I see your updated question, but I'm not clear what behavior do you want your page to achieve. From your first image, are you having one section that shows only one entry to fill in(the one with name, port, etc) and another section to add as many form as a user wants? Are these 2 sections related? Does the first section only has one entry always? – Shang Wang Oct 08 '15 at 13:29
  • the first picture is a template populated form a database, what I want my template for is if the user wants to add another empty form. MY template consists of 1 Device form and dynamic amount of register forms(formset). When I want the user to be able to dynamically add a new device form(with registers). Hence why the second picture shows only 1 row of registers because it's an empty from for them to fill out – john Oct 08 '15 at 13:37
  • Have you looked at this question? http://stackoverflow.com/questions/20894629/django-nested-inline-formsets – Shang Wang Oct 08 '15 at 13:40
  • I have, still trying to figure out to applying it – john Oct 08 '15 at 14:08