user2424607
user2424607

Reputation: 295

django templates - passing variables between templates

I have one template that includes a popup page. I loop through all values in a list that I get from the view and want to include information from each element of the list in the popup.

page1.html:

{% include "popup.html" %}
...
{% for element in someList %}
    <div class="col s3 some-button"><a onclick="$(showPopup('popup'));">More Info</a></div>
{% endfor %}

showPopup is a Javascript function that will show the popup.

In popup.html I reference element from the for loop above:

popup.html

...
{{ element }} 
...

But the popup.html template does not seem to be able to find the element from the for loop, since nothing is shown. Is there a way to make it so the popup.html is able to reference element?

Upvotes: 3

Views: 2467

Answers (2)

user2424607
user2424607

Reputation: 295

I found using this worked for my purposes:

page1.html:

...
{% for element in someList %}
    {% include "popup.html" with element=element %}
    <div class="col s3 some-button"><a onclick="$(showPopup('popup'));">More Info</a></div>
{% endfor %}

Upvotes: 5

ettanany
ettanany

Reputation: 19806

In your link you can add an attribute data-element="{{ element }}" and in the onclick method associated with the link you can retrieve the value like this:

var el = $(this).data("element");

Now, pass el to your popup.

Upvotes: 0

Related Questions