Brian Mayfield
Brian Mayfield

Reputation: 11

ImportError: cannot import name 'templates' from flask

Currently I am working on a website, and everything seemed to go all right until I got this error:

ImportError Command-Line Prompt Screenshot

# lab6_brian_may.py

"""This is a simple website program."""

# imports
from flask import Flask, request, url_for, redirect, render_template, templates

app = Flask(__name__, template_folder='../pages/templates')

# base site app route
@app.route('/')
def base_site():
    return render_template(templates + 'base.html')
 return ok

# RPG site app route
@app.route('/RPG', methods=['GET', 'RETURN'])
def RPG_site():
    if request.method == 'RETURN':
        # redirect to base page
        return redirect(url_for('base_site'))
    return render_template(templates + 'RPG.html')
 return ok

# MMO site app route
@app.route('/MMO', methods=['GET', 'RETURN'])
def MMO_site():
    if request.method == 'RETURN':
        # redirect to base page
        return redirect(url_for('base_site'))
    return render_template(templates + 'MMO.html')
 return ok

# First-Person Shooter site app route
@app.route('/First-Person Shooter', methods=['GET', 'RETURN'])
def first_person_shooter_site():
    if request.method == 'RETURN':
        # redirect to base page
        return redirect(url_for('base_site'))
    return render_template(templates + 'First-Person Shooter.html')
return ok

Any help on this issue is much appreciated.

Upvotes: 0

Views: 2651

Answers (2)

Anandakrishnan
Anandakrishnan

Reputation: 379

Actually there is no pre-built class like templates in Flask. You only need to do the following:

  1. Create a directory named templates
  2. Add the html file under the templates folder.
  3. For rendering template, use the following code : return render_template('template name.html')

Upvotes: 1

vremes
vremes

Reputation: 674

There is no such thing as templates in flask module, that is why it raises ImportError.

Flask will look for templates in templates folder by default when you call render_template.

Have you tried simply changing your render_template calls from return render_template(templates + 'MMO.html') to return render_template('MMO.html')?

Upvotes: 1

Related Questions