jinja2.exceptions.TemplateNotFound error when making a python virtual environment

Question:

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 class="menu">
            <li><a href="{{ url_for('home') }}">Home</a></li>
            <li><a href="{{ url_for('about') }}">About</a></li>
          </ul>
        </nav></strong>
      </div>
    </header>
    <div class="container">
        {%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!

Asked By: codaKid

||

Answers:

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)
Answered By: collinsmarra

If use virtual environment You must :

  1. first active virtual environment
  2. go to directory app.py file
  3. run python app.py

For example: my project flask in this path D:MyProjectsMyflaskApp first active virtualEnviorment then :

(my_env) E:virtualEnc> cd "D:MyProjectsMyflaskApp"
(my_env) D:MyProjectsMyflaskApp>python app.py
* Serving Flask app '__name__'
* Debug mode: off

app.py :

app = Flask(__name__)
Answered By: henrry