Flask MVC - doubts

0

I have doubts about making my application as organized as possible, I'm having a hard time implementing MVC ...

Thisisthefilethatlaunchestheapplication

fromflaskimportFlaskapp=Flask(__name__,template_folder='template')app.run(debug=True)

ThisistheViewsfile

fromflaskimportrender_template,redirect,requestfromAppimportappfromsettingsimportREPOSITORY_NAME,[email protected]('/')@app.route('/home')defhome():returnrender_template('template.html')

Ijustcannotgetthetempleterenderedbyrender_templategivesa404error.WhatcanIbedoingwrong?

Imanagedtomakeitworkthatway:

runservice.py stayed that way

from project import app

if __name__ == '__main__':
    app.run(debug = True)

In the Controllers folder we have init .py and views.py

init .py

 import os, glob
    __all__ = [os.path.basename(f)[:-3]
    for f in glob.glob(os.path.dirname(__file__)+'/*.py')]

Views.py

from project import app
from flask import render_template, request
from flask_wtf import FlaskForm
from wtforms import StringField
from wtforms.validators import DataRequired


class CreateForm(FlaskForm):
    texto = StringField('name', validators=[DataRequired])

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

In the root of the proyecct folder we have init .py

from flask import Flask
app = Flask('project')
app.debug = True


from project.controllers import *

PS: I do not know if this is the best way to work, but I got the expected result! If someone has a better solution I would like to know!

    
asked by anonymous 08.09.2018 / 19:17

2 answers

1

You have defined a template folder with the name of template here: template_folder='template'

But by the image you can verify that the folder you created is called templates in the plural!

So maybe it's just a matter of just getting it right, renaming the folder or changing the code.

    
09.09.2018 / 00:39
1

See how ocomunitário was done.

link

I try to follow good practice every time.

Understand one thing, this is your code:

app = Flask(__name__,template_folder='template' )

You ask flask to see a folder called template, but its folder shown in the image is named: templates with the end, so it can not find the file and error 404.

    
09.09.2018 / 15:40