Flask MVC - doubts

Asked

Viewed 724 times

0

I have doubts about making my application as organized as possible, I’m having trouble implementing MVC ... <code>inserir a descrição da imagem aqui</code>

This is the file that starts the application

from flask import Flask

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

app.run(debug=True)

This is the collection of Views

from flask import render_template, redirect, request

from App import app

from settings import REPOSITORY_NAME, REPOSITORY_SETTINGS


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

Only I can’t get Templete to be rendered by render_template of a 404 error. What I might be doing wrong?


I managed to make it work that way:

inserir a descrição da imagem aqui

runservice.py stayed that way

from project import app

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

Inside the Controllers folder we have the 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 projectct folder we have the init.py

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


from project.controllers import *

PS: I don’t know if it’s the best way to work, but I got the result! In case anyone has a better solution would like to know!

2 answers

1

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

However through the image it is possible to verify that the folder you created is called templates plural!

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

1

See how the ocomunitário was made.

https://github.com/DwarfTeam/ocomunitario

I try to follow good practices always.

Understand something, this is your code:

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

You ask flask to view a folder called template, but its folder shown in the image is named after : templates with s at the end, so it fails to find file and error 404.

  • I switched to templetes and still gave 404

  • take off the template_folder='template' and tell me what comes back when you try to access it. Just taking a question with you, inside the templates folder, you have? printer/index.html? remembering that Printer is a folder and index.html has it inside Printer.

  • I had to change the whole structure that the project was in, I took that template_folder = templete , and put the Printer/index.html there worked

  • so if I helped you, please rate me as the best answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.