Reputation: 2589
i need to send some data to a template in the format of
template_name: RTR-01
device_name: EDGE
templte_name: RTR-02
device_name: EDGE
template_name: SW-01
device_name: CORE
and so on.
i know i cant use a list as they only allow one value, would a class be best suited? and how do i create a class on the fly? also this data is not stored in the DB its built on the fly
EDIT:
Current config for the dynamic data below, i take the template name which is in the format STR-RTR-01, then i put the location name in the middle to make STR-UK-RTR-01. in my dictionary i need both the before and after result, how do i create the dictionary on the fly in this manner?
showroom_devices = []
for item in config_templates:
if item['device_name'].startswith("STR-"):
device = item['device_name']
completed_device_name = device[:4] + modelShowroom.location.upper()[:4] + "-" + device[4:]
showroom_devices.append(completed_device_name)
Thanks
Upvotes: 0
Views: 131
Reputation: 494
You can store your data in a list of dictionaries:
data = [
{
"template_name": "RTR-01",
"device_name": "EDGE"
},
{
"template_name": "SW-01",
"device_name": "CORE"
},
]
Then you will need to update the template's context by overriding the render_to_response
method.
def render_to_response(self, context, **kwargs):
context.update({"data": data})
return super().render_to_response(context, **kwargs)
And then you can iterate in your template
<ul>
{% for item in data %}
<li>{{item.template_name}}</li>
<li>{{item.device_name}}</li>
{% endfor %}
Upvotes: 1
Reputation: 5554
A list
of dict
s seems best suited:
context = [
{
'template_name': 'RTR-01',
'device': 'EDGE',
},
{
'template_name': 'RTR-02',
'device': 'EDGE',
},
{
'template_name': 'SW-01',
'device': 'CORE',
},
]
You use classes when you want to associate behaviour to your data, which does not seem to be the case here.
Upvotes: 0