For some reason, I keep getting an error detected on the index.html file whenever I add url_for(). This happens whether I add the function to only index.html, only more.html, or both.
More.html seems to accept the function without any problems however.
The code works only for the second method. I cannot get the first method working as soon as a url_for() is added to the code.
app.py:
import datetime
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
now = datetime.datetime.now()
new_year = now.month == 1 and now.day == 1
return render_template("index.html", new_year=new_year)
@app.route('/<string:name>')
def more(name):
name = name.capitalize()
defaultNames = ["Albert", "Michelle", "Ivette", "Sylvia"]
return render_template("more.html", name=name, defaultNames=defaultNames)
index.html:
<!DOCTYPE html>
<html>
<head>
<title>Python Tutorial</title>
</head>
<body>
{% if new_year %}
<h1>It's NEW year's</h1>
{% else %}
<h1>NOT NEW YEAR'S</h1>
{% endif %}
<a href="{{ url_for('more') }}">names page</a>
</body>
</html>
more.html:
<!DOCTYPE html>
<html>
<head>
<title>names page</title>
</head>
<body>
<h1>hello, {{name}}!</h1>
<ul>
{% for name in defaultNames %}
<li>{{ name }}</li>
{% endfor %}
</ul>
<a href="{{ url_for('index') }}">Go back</a>
</body>
</html>
I see "Internal server error" whenever I try to run the code with the url_for() function. The second method works fine though.