程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python flask lightweight web framework

編輯:Python

precondition :

You are already in Windows It's finished Pycharm Integrated development tools +Python3.6 Version of and related third-party packages

Step1: Whole frame


Step2: Structural description of the frame


Project structure

- manage.py  Used to control the program
  python manage.py runserver # Start service command
  python manage.py shell # Get into shell Script mode commands
- app/__init__  Initialization file
   Initialize the whole Flask object , as well as Flask Various plug-ins used , And return a app application
- app/settings  Project profile
   Configure the entire project operation environment
- app/ext  Extension library of the project
   Third party extension library packaging
- app/views  The view function
   Process business logic
   Coordinate the relationship between modules and models
- app/models   Model
   Define the model structure
   Get the relational mapping of the tables in the database
  db.create_all()  Create database
  db.drop_all()  Delete database
- doc  Framework description document
- static  Directory for storing static resources
- templates  The directory used to store template resources
- migrations  Migration Directory


Data migration

-  Map the model to the database
-  Use flask-migrate library
-  Installation and use
  pip install flask-migrate
   initialization , Need to use app and db To initialize ,migrate=Migrate(app,db)
   To configure flask-script  The order of
    manager.add_command('db',MigrateCommand)
-   Instructions to use
  python manage.py db init
    init  Initialization instruction , Can only be called once
  python manage.py db migrate
    migrate Instructions are used to generate migration files
     Internal migration files use linked lists to associate relationships
    --message MSG  Add log description for migration
  python manage.py db upgrade
    upgrade Instructions are used to execute migration files
     Database content upgrade
  python manage.py db downgrade
    downgrade Instructions are used to execute migration files
     Database content degradation , Equivalent to regret medicine
-  help  Help document

Step3: The general call logic is as follows


<1>  App/views.py  The file introduces models.py【 Model objects in the model package 】andext.py【 In the expansion package db  object 】

<2>  App/__init__.py  The file introduces views.py【 The blueprint in the view package 】,ext.py【 Introduce the expansion package and do initialization work in the expansion package 】,settings.py【 Introduce the setting package and do the database connection, environment configuration and resource directory settings 】. Finally, initialization is completed app  After the application , Return to one app application .

<3>  manage.py It's the entry file , Get the one returned in the previous step app application , Finally, start the service .

Step4: Effect of visiting the home page after service startup

Step5: For the above figure, see the source code of routing and page


In fact, it is to render Templates/home.html page

App/views.py:      

@blue.route('/index/',methods=['GET','POST'])
def index():
    return render_template('home.html')

The page is mainly the two links of login and exit

Templates/home.html  The page source code is :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h2>welcome back:{
{ username }}</h2>
<a href="{
{ url_for('second_blue.login') }}">login</a>
<a href="{
{ url_for('second_blue.logout') }}">logout</a>
</body>
<html>

Welcome to your attention 【 The way of immeasurable testing 】 official account , reply 【 Claim resources 】

Python+Unittest frame API automation 、

Python+Unittest frame API automation 、

Python+Pytest frame API automation 、

Python+Pandas+Pyecharts Big data analysis 、

Python+Selenium frame Web Of UI automation 、

Python+Appium frame APP Of UI automation 、

Python Programming learning resources dry goods 、

Vue Front end component framework development 、

Resources and code Free ~
Below the official account is two-dimensional code. , You can directly scan wechat and pay attention .

remarks : My official account has been officially opened. , betake IT Sharing of Internet technology .

contain : Data analysis 、 big data 、 machine learning 、 Test Development 、API Interface automation 、 Test operation and maintenance 、UI automation 、 Performance testing 、 code detection 、 Programming technology, etc .

WeChat search official account :“ The way of immeasurable testing ”, Or scan the qr code below :

   

   Add the attention , Let's grow together !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved