0

Here is my Python and HTML code to display "happy birthday" on the day of birthday and "no" on rest of the days.

Application.py

import datetime
from flask import Flask, render_template

app = Flask(__name__)

@app.route("/")
def ind():
    now = datetime.datetime.now()
    birthday = now.month == 9 and now.day == 16
    return render_template("ind.html", birthday = birthday)

ind.html

<!doctype html>
<html>
    <head>
      <title>Is it the birthday?</title>
      <style>
          body{
            text-align: center;
          }
      </style>
    </head>
    <body>
      {%if birthday%}
      <h1>Happy Birthday</h1>
      {%else%}
      <h1>No</h1>
      {%endif%}
    </body>
</html>

Errors i am getting:

* Serving Flask app "application"
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
[2020-05-02 10:42:47,258] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/lib/python3/dist-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/lib/python3/dist-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python3/dist-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/usr/lib/python3/dist-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/lib/python3/dist-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/heisenberg/my_flask_app/application.py", line 10, in ind
    return render_template("ind.html", birthday = birthday)
  File "/usr/lib/python3/dist-packages/flask/templating.py", line 133, in render_template
    return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
  File "/usr/lib/python3/dist-packages/jinja2/environment.py", line 869, in get_or_select_template
    return self.get_template(template_name_or_list, parent, globals)
  File "/usr/lib/python3/dist-packages/jinja2/environment.py", line 830, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/usr/lib/python3/dist-packages/jinja2/environment.py", line 804, in _load_template
    template = self.loader.load(self, name, globals)
  File "/usr/lib/python3/dist-packages/jinja2/loaders.py", line 113, in load
    source, filename, uptodate = self.get_source(environment, name)
  File "/usr/lib/python3/dist-packages/flask/templating.py", line 57, in get_source
    return self._get_source_fast(environment, template)
  File "/usr/lib/python3/dist-packages/flask/templating.py", line 85, in _get_source_fast
    raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: ind.html
127.0.0.1 - - [02/May/2020 10:42:47] "GET / HTTP/1.1" 500 -

I have run the following commands:

  1. To create a virtual environment:

    source venv/bin/activate

  2. To run my python file.

    export FLASK_APP=application.py

    flask run

Directory structure of templates:

my_flask_app/venv/lib64/python3.6/site-packages/flask

Directory structure of main.py:

my_flask_app/venv/lib64/python3.6/site-packages/flask

Please make me aware of my mistakes.

Thanks in advance.

jai
  • 112
  • 1
  • 7

2 Answers2

1

This question already has an answer here: Flask Template Not found

Flask search for templates folder by default for html contents which is under the same root of Application.py by default. If it doesn't get the templates folder then it shows error. Instead of letting flask search it by default, you should declare it explicitly.

app = Flask(__name__,
           template_folder='templates', # your templates folder name goes here
           static_folder='static',   # your static folder name goes here
           )

And to avoid Folder/File Not found i would suggest giving full absolute path in those parameters. You can also check if you have bug-like template and templates folder naming.

Tips: As you mentioned, Long Errors. A long error message happens when there are a lot of dependencies in your code. This means if you just miss one thing the rest also blow up! Try to check the message/line number in your py file and not in 3rd party packages message/line number. Checking the last couple of lines of error works in 80% of the time, as this line started the error.

mrx
  • 364
  • 1
  • 3
  • 9
0

You should create a templates directory in which your html files are stored

The templates directory must be on the same level as the python file

-templates
    -ind.html
-main.py
DevHyperCoder
  • 895
  • 1
  • 9
  • 22