2

everyone, i met a problem in Flask. There is a table in mysql named category, I queried all of them, then passed them to a.html, like this :

return render_template('admin_index.html', username=session.get('username'), categories=categories)

In admin_index.html, I wanna list all of them, here is my code:

var _menus = {
    "menus": [
        {
            "menuid": "1",
            "icon": "icon-sys",
            "menuname": "category",
            "menus": [
                {% for category in categories %}
                    {
                        "menuid": "{{ category.id }}",
                        "menuname":"{{ category.name }}",
                        "icon": "icon-users",
                        "url": "{{url_for('admin.category', id={{category.id}} _external=True) }}"
                    },
                {% endfor %}
            ]
        }
    ]
};

but when i ran this, i got an error:

jinja2.exceptions.TemplateSyntaxError: expected token ':', got '}'

I tried remove {{ category.id }}, turned out OK, I suspect it's not allowed to nest variables in Flask template, which like this :

{{ a is {{ b }} }}

Is there any way i can nest it?

0

1 Answer 1

5

You are already inside a jinja expression. So the following code is executed:

url_for("admin.category", id={{category.id}} _external=True)

Because of the { it expects a dictionary hence the error.

To use the value of category.id simply omit the curly brackets:

url_for("admin.category", id=category.id, _external=True)

Also note that you forgot a , before "_external".

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.