Home > Net >  jinja2.exceptions.TemplateNotFound error when making a python virtual environment
jinja2.exceptions.TemplateNotFound error when making a python virtual environment

Time:11-03

so I have made a website using Flask which was working perfectly fine until today when I tried to create a python virtual environment. Does anyone know what could have happened? This is the code for my python file.

from flask import Flask, render_template

app = Flask(__name__, template_folder='html_scripts')

@app.route('/home/')
def home():
    return render_template('html_scripts')

@app.route('/about/')
def about():
    return render_template('html_scripts')

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

This is the code for my main html file:

<!DOCTYPE html>
<html>
  <head>
      <title>Flask App</title>
      <link rel="stylesheet" href="{{url_for('static',filename='css/main.css')}}">
  </head>
  <body>
    <header>
      <div class="container">
        <h1 class="logo">Adrian's web app</h1>
        <strong><nav>
          <ul >
            <li><a href="{{ url_for('home') }}">Home</a></li>
            <li><a href="{{ url_for('about') }}">About</a></li>
          </ul>
        </nav></strong>
      </div>
    </header>
    <div >
        {%block content%}
        {%endblock%}
    </div>
  </body>
</html>

And these are the remaining 2 files(home_page.html and about.html):

{%extends 'layout.html'%}
{%block content%}
<div class = 'home'>
    <h1>My homepage</h1>
   <p>This is my homepage</p>
</div>
{%endblock%}

and,

{%extends 'layout.html'%}
{%block content%}
<div class = 'about'>
    <h1>My about page</h1>
    <p>This is my about page</p>
</div>
{%endblock%}

Please do help if you kow how because I have not seen anyone with this problem yet and I can't solve it myself. Thanks!

CodePudding user response:

In render_template, try using the actual html files. The code below fixes your problem

from flask import Flask, render_template

app = Flask(__name__, template_folder='html_scripts')

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

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

if __name__ == '__main__':
    app.run(debug = True)
  • Related