This section is taken from the second half of part I Chapter 2, skipping over the details of the specific interaction design for request and response. Mainly through the Flask-script plug-in Let the reader for the plug-in system has a simple understanding. Flask is focused on extensibility, there are many plugins available in the community, and you can use the Python standard library or a variety of other l
Flask form (Flask-WTF) and flask form flask-wtf
1. request. from: Get POST form data
# Hello. py
1 # coding: UTF-8 2 3 from flask import Flask, request, render_template 4 5 app = Flask
', Lazy='Dynamic')
classAddress (db. Model): ID= db. Column (db. Integer, primary_key=True) Email= db. Column (db. String (50))
#use DB on one side. ForeignKey declaring foreign keysperson_id = db. Column (db. Integer, Db. ForeignKey ('person.id'))
I. Configuration Flask-migrate from Import Migrate, Migratecommand
= Migrate (app,db)
# The first argument is an instance of flask, and the second
Development Environment Establishment of the Flask framework Learning Guide and flask Learning Guide
Flask is a lightweight Web application framework written in Python. Its WSGI toolbox uses Werkzeug, And the template engine uses Jinja2. Many functions are implemented based on the django framework. As required by the project, record the learning process and exper
Flask-Migrate can help the Flask application to complete database migration through a preset Python script. here we will take a look at the tutorial on using Flask-Migrate to expand database migration in the Python Flask framework, when upgrading the system, you may need to update the data structure on the server. the
{% Extends "base.html" %}
{% Block content %}{% For page in pages %}{% Else %}{% Endfor %}{% Endblock content %}Copy codeThe Code is as follows:$ Vi app/templates/page.html{% Extends "base.html" %}
{% Block content %}{Page.html | safe }}{% Endblock content %}The Flask-FlatPages module searches for the Markdown document ending with. md from the pages directory by default. Therefore, we put the content of static blogs in this directory:Copy codeThe Code
Tags: Introducing model ROM column REM move use register modSteps to use: 1. Introduction of Flask-sqlalchemyfrom
import SQLAlchemy
= SQLAlchemy () 2. Register flask-SQLAlchemy
db.init_app (APP) 3. Import the tables
in models from Import * 4. Write class inherits Db. Model
class Users (db. Model):
__tablename__'users'
= Column (Integer, Primary_key=true) 5th step:
Installing PIP3
In the Python Flask framework, use Flask-Migrate to expand the database migration tutorial, flaskflask-migrate
When upgrading the system, we often encounter operations such as updating the data structure on the server. The previous method was to manually write an alter SQL script for processing, and we often find omissions, as a result, the program cannot be used normally after being published to the server
Flask is a very flexible and short-witted web framework, so where does flexibility manifest itself?There is a magical thing called flask configuration, how to use this thing? What kind of convenience can it bring us?First show: from Import = Flask (__name__) # type:flaskapp.config["DEBUG" ] = TrueThis sentence app.config["DEBUG"] = True can be achieved by the
migration warehouseFirst, we want to install flask-migrate in a virtual environment:Pip Install Flask-migrateThe initialization method for this extension is as follows:from = Migrate (app, db) Manager.add_command ('db', Migratecommand)To export the database Migration command, Flask-migrate provides a Migratecommand cl
processing the first request.
Before_request registers a function and runs it before each request.
After_request registers a function. If no unprocessed exception is thrown, it runs after each request.
Teardown_request registers a function and runs after each request even if an unhandled exception is thrown.
④ Response
@app.route('/')def index(): return '
from flask import make_res
configuration file from the project
Modify _ init _. py: Add the following content (in red ):
#-*-Coding: UTF-8-*-from flask import Flaskfrom flask_sqlalchemy import SQLAlchemyapp = Flask (_ name _) # import OS # print OS. environ. keys () # print OS. environ. get ('flaskr _ settings') # load the configuration file content app. config. from_object ('blog2. setting ') # The setting file name in the module.
Flask series tutorials (8) -- Flask-Migrate, flaskflask-migrateFlask-Migrate
In the actual development environment, database changes often occur. Generally, we do not manually modify the database, but modify the database.ORMThe corresponding model, and then map the model to the database. At this time, it would be very useful to have a tool dedicated to this kind of thing.flask-migrateThis is what we do.flas
.
Many extensions have been developed in the community for various purposes. if this is not enough, you can use any Python Standard package and library. To let you know how an extension is incorporated into an application, the following section adds an extension to hello. py and adds the command line parameters of the application.
6.1. Flask-Script command line o
application.
Then enter the following command to start the application:
(venv) $ python hello.py * Running on Http://127.0.0.1:5000/* Restarting with Reloader
If you enter any other URL, the application will not know how to manipulate it and will return an error code 404 to the browser-when you access a nonexistent Web page you will get the error.
The enhanced version of the application shown below adds a second dynamic route. When you visit this
Blog Project Source: Https://github.com/JmilkFan/JmilkFan-s-Blog Directory
Table of Contents list put request delete request test Update operation on an existing posts record delete a record
List of the preceding text
Use flask to write a light blog (1)-Create a projectUse flask to write a light blog (2)-hello world.Use flask to write a light blog (3)-(M) vc_ con
HttpResponse in 1.FlaskThe HttpResponse in flask, in our view, is actually a direct return string.REDIRECT in 2.FlaskWhenever the address "/redi" is accessed, the View function Redi will trigger redirect ("/") to jump to the URL address: "/" and will trigger the "/" Corresponding view function index ()Render (Render_template) in 3.FlaskHTML template rendering is a must in every web framework, as for the specific use of render_template, leave a suspens
from Flask import flask App = flask (__name) @app. Route ('/ ') def index (): return ' if __name__ = = '__main__': App.run ( Debug = True)Then execute Pyhont hello.py in the command line and enter 127.0.0.1:5000/in the browser to see the word HelloWorld.Code AnalysisFirst lineImport
87. flask-script component of flask, flaskflask-script
The Flask Script extension provides the ability to insert external scripts to Flask, including running a development server, a custom Python shell, setting database scripts, cronjobs, and other command line Tasks running
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.