Django template issue

I have a need to add a customized page to django admin.
Thus I used TemplateView and passed a list of models to the template.
Here’s the model

class Event(models.Model):
    title = models.CharField()
    status = models.CharField()
    receiver = models.ForeignKey()
    topic = models.ForeignKey()
    data = JSONField()
    ...

I show the list of items in and tags.

<select name="events" id="events">
    {% for n in events %}
       <option value="{{ forloop.counter0 }}">{{ n.pk }} - {{ n.title }}</option>
    {% endfor %}
</select>

And when the user selects an item from the list, I want to show more details of the selected item - status, receiver, topic, data, etc.
I don’t think i need to make a request to pull the data since I already pulled it on page load.
I can get index of selected item, thus I think I can get data in javascript.
I know how to write custom template filters to get data from list by index.
But now sure how I can get that events data in javascript.
Javascript code with this situation would be helpful.
Thanks

It’s only available to you if you have somehow rendered it in your template. The view is executed on the server, but all the JavaScript runs in the browser. Data “left behind” on the server is not available.

You could render that extra data in a hidden div or as a JavaScript data object. Either way, it needs to be included in the page if you want to use it without an additional request.

I’m not sure I understand what you’re asking for here.
But pretty much, the specifics of doing that is going to depend upon what (if any) JavaScript framework you may be using in the front-end.

1 Like