I will omit the installation of Flask.
By default, the startup file is runserver.py. The role of this runserver.py file is to "only" start the app by executing the "app.run" function.
There is a file called "init.py" directly under the "_201114FlaskScraping" folder in Solution Explorer. If you create a file with this name, you will be able to import _201114FlaskScraping as a package. "Runserver.py" just imports "_201114FlaskScraping package" and starts it. 「from _201114FlaskScraping import app」
runserver.py
"""
This script runs the _201114FlaskScraping application using a development server.
"""
from os import environ
from _201114FlaskScraping import app
if __name__ == '__main__':
HOST = environ.get('SERVER_HOST', 'localhost')
try:
PORT = int(environ.get('SERVER_PORT', '5555'))
except ValueError:
PORT = 5555
app.run(HOST, PORT)
init.py has two main roles. ① Represents the directory where the python script is located (2) Describe the initialization process such as importing the required module and initialize it.
__init__.py
"""
The flask application package.
"""
from flask import Flask
app = Flask(__name__)
import _201114FlaskScraping.views
view.py
"""
Routes and views for the flask application.
"""
from datetime import datetime
from flask import render_template
from _201114FlaskScraping import app
@app.route('/')
@app.route('/home')
def home():
"""Renders the home page."""
return render_template(
'index.html',
title='Home Page',
year=datetime.now().year,
)
@app.route('/contact')
def contact():
"""Renders the contact page."""
return render_template(
'contact.html',
title='Contact',
year=datetime.now().year,
message='Your contact page.'
)
@app.route('/about')
def about():
"""Renders the about page."""
return render_template(
'about.html',
title='About',
year=datetime.now().year,
message='Your application description page.'
)
"Method =" POST "action =" / about "" and throw it to the server
index.html
<form method="POST" action="/about">
<label>text:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Name">
<button type="submit">Send</button>
</form>
Receive as "@ app.route ('/ about', methods = ['POST'])"
views.py
@app.route('/about',methods=['POST'])
def about():
"""Renders the about page."""
return render_template(
'about.html',
title='About',
year=datetime.now().year,
message='Your application description page.'
)
The / about page is displayed
The basic policy is to increase the functionality of "views.py". However, since the program becomes complicated, I will use "BluePrint" ... (continued)
Recommended Posts