I'm trying to build a simple flask page that displays links from a dictionary of text/links:
urls = {'look at this page': www.example.com, 'another_page': www.example2.com}
@app.route('/my_page')
def index(urls=urls):
return render_template('my_page.html',urls=urls)
My template page looks like this:
{%- block content %}
{%- for url in urls %}
<a href="{{ url_for(urls.get(url)) }}">{{ url }}</a>
{%- endfor %}
{%- endblock content %}
I can't quite seem to understand how to create dynamic urls like this. The code produces this error:
TypeError: 'NoneType' object has no attribute '__getitem__'
Can anyone point out my problem or a solution?
UPDATE: Here's my updated code:
@app.route('/my_page')
def index():
context = {'urls': urls}
return render_template('index.html', context=context)
And the template:
{%- block content %}
{% for key, data in context.items() %}
{% for text, url in data.items() %}
<a href="{{ url }}">{{ text }}</a>
{% endfor %}
{% endfor %}
{%- endblock content %}
This solution is close, however each link get prepended with my app's url. In other words I get this:
<a href="http://127.0.0.1:8000/www.example.com">look at this page</a>
I just want:
<a href="http://www.example.com">look at this page</a>