0

I'm new to python and Flask, and I'm trying to setup a basic page to render an HTML template. I followed the instructions/tutorials and I'm able to render HTML via the use of render_template like so:

app = Flask(__name__)

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

I'm trying to now run a sample app, where the index.html in the app has actually a reference to another .html page, like so:

../../html/index.html

When I load this, I just see this text - literally. I assume it's trying to load the index.html from referenced folder (which I verified exists).

What is missing here? Am I missing a module or something?

EDIT:

This is where the main .py script is that I am running.

enter image description here

the index.html is in the templates directory. And then two folders up is the html folder that it seems to be referencing:

enter image description here

I know based on this, it should be "../html/index.html", but I've tried that, too and that doesn't work either.

M.R.
  • 4,737
  • 3
  • 37
  • 81
  • 1
    Show us the template code and the project structure! – Klaus D. Jul 26 '20 at 04:12
  • You likely need to [`include`](https://stackoverflow.com/a/22860144/4032503) you html file in the template, but without seeing your template code as @KlausD. asked for, it is hard to know for sure. – noslenkwah Jul 27 '20 at 15:30
  • When you say template code, you mean what's in the index.html file? – M.R. Jul 27 '20 at 19:08

2 Answers2

0

Static files in templates should be inserted using url_for function as described in Flask docs. Your case would be:

url_for('../../html/index.html')
Carlos Bazilio
  • 830
  • 9
  • 20
  • This did not work... now I just see the literal text `url_for('../../html/index.html')` – M.R. Jul 27 '20 at 14:39
  • Have you seen [this thread](https://stackoverflow.com/questions/31002890/how-to-reference-a-html-template-from-a-different-directory-in-python-flask/31003097)? – Carlos Bazilio Jul 27 '20 at 17:36
0

I suppose you can do it in two ways url_for('templates',filename='index.html') where templates will be folder in which html files will be stored and secondly you can use extend feature of jinja2 {% extends index.html %}