-1

I have a dictionary with two dictionaries inside, with key-value pairs. I am trying to get the URL dynamically so that, when I display a bulletlist of dict names, I click them and I get a page with the related key-value pairs. So that suppose I have

cities = { 'Moskow': {'Russia':'RU'}, 'London': {'United Kingdom':'UK'}

On the index.html page is displayed:

  • Moskow
  • London

I click on Moskow and I get

  • Russia, RU

But when I click on London I get

  • United Kingdom, UK

I tried using url_for in index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ title }}</title>
</head>
<body>
<h1>Cities</h1>
<ul>
        {% for city, city_info in cities.items(): %}
    <li><a href={{url_for({{city}})}}>{{city}}</a></li>
    {% endfor %}
    </ul>
</body>
</html>

And route is

@app.route('/')
@app.route('/index')
def index():
    citydict = cities
    return render_template('index.html', title='Welcome', cities=citydict)
    
    @app.route('/city')
    def city():
        citydict = cities
        return render_template('cities.html', title='Welcome', cities=citydict)

But it doesn't work

davidism
  • 121,510
  • 29
  • 395
  • 339

1 Answers1

-1

Edited

It won't work because you don't have any defined functions named "Moskow" or "London".

By your logic, your html file will have something like - url_for('Moskow') and url_for('London') but you don't have any routes or anything like define Moskow() or define London()

Updated:

See this stackoverflow answer for how to use url_for

NoCommandLine
  • 5,044
  • 2
  • 4
  • 15