0

I'm new to Flask and looking for a way to parse one or n values of a jinja variable.

The app route hands an object over to the html template.

`selection2 = db.execute("SELECT user.id, bikes.id, bikes.name, bikeshopname, price, city, img, enddate FROM user LEFT JOIN bikes ON user.id = bikes.userid LEFT JOIN renthistory ON bikes.id = renthistory.bikeid WHERE user.city = :city AND price NOT NULL", city=select_city)
    return render_template("/bikes.html", selection2=selection2)`

In the template I implemented it like this:

{% for row in selection2 %} <p id="enddate" value="{{row["enddate"]}}"></p> {% endfor %}

In the js function I get the value (a certain date) of that particular paragraph. So it works for the first item.

function getBikeRentEndDate() {
  let enddate = (document.getElementById("enddate").innerHTML);
  document.getElementById('showdate').innerHTML = enddate;
}

But what I want is to look in all the created once the template is rendered. And then show a message if a condition is true. Something like "if date is => today - show a message.

I know there is a way but after researching and trail and erroring for quite a while, I hope someone can point it out.

1
  • All your paragraphs have the same id. Ids should be unique. You should use classes instead Commented May 29, 2020 at 15:57

1 Answer 1

1

To achieve the desired result, you have to first cast the jinja2 variable to js.

To do that in your template:

<script>
    const listselection2 = JSON.parse('{{ selection2 | tojson | safe }}');
    //do your stuff with listselection2.
</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.