0

I'm new to flask/python and I'm trying to debug someone else's tutorial. This is the structure of all of their files:

/flask_project
    /app.py
    /myprojects
        /__pycache__
        /__init__.py
        /forms.py
        /models.py
        /base.html
        /login.html
        /models.html
        /register.html
        /home.html

But it's giving me a "flask template not found" error when trying to access the first required html file - home.html. I tried renaming the myprojects folder to templates, but that didn't help. I was confused by the flask documentation as well - I have an init.py file with code in it, but the app.py itself is a module I think. Here is the code for app.py:

# app.py
from myprojects import app, models, db
from flask import render_template, redirect, request, url_for, flash, abort
from flask_login import login_user, login_required, logout_user
from forms import LoginForm, RegistrationForm 

@app.route('/')
def home():
    return render_template('home.html')

@app.route('/welcome')
@login_required
def welcome_user():
    return render_template('welcome_user.html')

@app.route('/logout')
@login_required
def logout():
    logout_user()
    flash("You logged out!")
    return redirect(url_for('home'))

@app.route('/login', methods = ['GET', 'POST'])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email = form.email.data).first()
        
        if user.check_password(form.password.data) and user is not none:
            login_user(user)
            flash('Logged in successfully!')
            
            next = request.args.get('next')
            
            if next == None or notnext[0] == '/':
                next = url_for('welcome_user')
                
            return redirect(next)
        
    return render_template('login.html', form=form)

@app.route('/register', methods=['GET', 'POST'])
def register():
    form = RegistrationForm(request.form)
    if request.method == "POST" and form.validate():
        user = User(email = form.email.data, 
            username = form.username.data, password = form.password.data)
        db.session.add(user)
        db.session.commit()
        flash("Thanks for registering!")
        return redirect(url_for('login'))
    return render_template('register.html', form=form)

if __name__ == '__main__':
    app.run(debug = True)
Rtrain
  • 45
  • 6
  • 3
    Unless you've specifically configured flask differently, it expects all templates to be available in a directory called `templates`. So I would try that :) – Wolph Aug 18 '20 at 20:03
  • I tried making a new directory `templates` alongside `myprojects`, with just the html files in it. I also tried renaming myprojects to templates. Neither solutions worked. Do I do something with `__init__.py`? – Rtrain Aug 18 '20 at 20:06
  • 2
    You'd need `myproject/templates` – OneCricketeer Aug 18 '20 at 20:06
  • Ah @OneCricketeer...so I just put my html files in `myproject/templates`, and keep `__init.__py`, `forms.py`, etc in just `myprojects`? – Rtrain Aug 18 '20 at 20:09
  • 1
    If all else fails, you can manually specify it like this as well: `app = Flask(__name__, template_folder='template')` – Wolph Aug 18 '20 at 20:09
  • 2
    Thanks a lot, creating `myprojects/templates` worked! – Rtrain Aug 18 '20 at 20:17

0 Answers0