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:
To create a virtual environment:
source venv/bin/activate
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.