return 0
return 0

Reputation: 4376

How do I create a very simple hyperlink on index.html with flask?

Here is my structure:

├── main.py
├── static
│   ├── css
│   │   └── main.css
│   ├── img
│   │   └── main_bkg.jpg
│   └── js
└── templates
    ├── myApp
    │   └── myApp.main.html
    └── index.html

I just want to create a very simple hyperlink on index.html that links to myApp.main.html.

My main.py looks like:

from flask import Flask, render_template
app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/<module>/')
def myApp(module):
    return render_template('{0}/{0}.main.html'.format(module)) 

@app.route('/user/<username>')
def show_user_profile(username):
    return 'User %s' % username

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)

In my index.html: I tried

<a href="{{ url_for('myApp') }}"><button>myApp</button></a>

The flask prompts BuildError BuildError: ('myApp', {}, None)

If I try the following on the myApp.main.html, then it provides a valid link back to home page:

<a href="{{ url_for('index') }}"><button>Home</button></a>

What did I miss here?

Upvotes: 0

Views: 7006

Answers (2)

Yahya Maamoun
Yahya Maamoun

Reputation: 1

I used the structure: ├── app.py ├── static └── templates ├── Homep.html └── index.html and I used the HTML:

<a href="{{ url_for('index') }}"><button>Home</button></a>

It worked fine with me.

Upvotes: 0

davidism
davidism

Reputation: 127340

Your myApp rule takes a module parameter. You didn't pass that to url_for, so it raised an error because it needs that information to build a url.

url_for('myApp', module='myModule')

Upvotes: 2

Related Questions