当前位置:网站首页>Flask project learning (I) -- sayhello
Flask project learning (I) -- sayhello
2022-07-26 09:00:00 【apprentice.】
Studying recently flask frame , On the one hand, the fastest way to learn is to read official documents . After reading the example project given by the official document , I also want to see some beginner projects to get familiar with this framework . therefore , As long as this series of blogs is to record their own learning flask The project source code viewed by the framework .
This article is mainly about sayHello project
1. Project brief introduction
project github The address is
https://github.com/greyli/sayhello
The project mainly consists of the following documents
according to README.md Explanation in , After installing the relevant environment dependencies , Start the project by
$ flask forge
$ flask run
* Running on http://127.0.0.1:5000/
After opening the page with a browser , This is the effect :
The functional logic of this project is relatively simple , It's mainly about sending messages , Then everyone can see .
2. Process explanation
We can read the relevant code step by step according to the steps just performed .
2.1 forge Method
First, before starting , We executed an order
flask forge
This command literally means , Give Way flask Framework to implement a called forge Methods , From the source code , We can search whether there is this forge Keyword code :
stay commands.py In file , There is indeed a man named forge Methods . By reading the notes , You can know that the main function of this method is to virtualize some information . And there's a count Parameters , It means how many messages should be virtual . For specific implementation , Is through a call Faker The object of , Afterlife output data , And then according to count Value to generate specific information :
for i in range(count):
message = Message(
name=fake.name(),
body=fake.sentence(),
timestamp=fake.date_time_this_year()
)
2.2 index Method
In our web page input corresponding IP:PORT after , Will jump to the home page . therefore , We can see how the code of the home page is implemented . stay view.py in , You can find that the route is ’/' Corresponding method :
This method includes two functions: front-end information submission and information display .
The first is to judge whether there is compliance submission , If there is a compliance submission , Just write the data into the database and reload the home page . For data submission , After getting the data from the front end , Will execute a name called flash Methods , This method mainly returns a prompt message to the front-end user . This message is displayed when rendering the template , Through get_flashed_messages() Method to get and render :
Before loading the home page , Will message After sorting the data , Pass in to the template to fill , And back to the front end :
messages = Message.query.order_by(Message.timestamp.desc()).all()
return render_template('index.html', form=form, messages=messages)
3. Entrance explanation
In this project , The entrance is __init__.py file
The first is to define this app The name is sayhello, And from settings.py The configuration file is loaded in . Then initialize the database according to the data of the loaded configuration file ,bootstrap etc. .
Finally, I will views, errors, commands Are loaded into the program .
Next , We can first look at what is written in the configuration file
First, according to different platforms , The database prefix is different . Then there's the setup SECRET_KEY, And database related configuration .
4. Other explanations
Because the logic of this project is relatively simple , I have just said all the code that the main process runs in this project . But there are also some codes that are not in the mainstream , We can talk about it alone .
4.1 errors.py
errors There are mainly two callback methods defined in , In the presence of 404,500 When , Call these two methods separately .
We can try it 404 The callback . Enter... In the browser ip:port, And follow a path that is not declared in the back end , You can 404
4.2 forms.py
forms It mainly defines a structure , It is the structure that the front end sends submission information to the back end . This structure inherits FlaskForm, Therefore, the main function of this structure is to transmit data at the front and rear ends . It is mainly divided into three attributes : full name , Content , Submit .
4.3 models.py
models There is mainly a Message class , This class is inherited from db.Model. Therefore, it can be guessed that this kind of main function is related to the storage database . Every record will become a Message object , And store it in a database
5. Harvest
Through this project , I mainly learned the following skills
5.1 flash
flash You can return some interactive information to the user . For example, when logging in to an account, whether it is successful or unsuccessful , You need to give users a prompt . adopt flash Data written , It can be used in the front-end template file get_flashed_messages() Method to get . We can do a verification .
stay view.py Write another interface
@app.route('/flash',methods=['GET','POST'])
def test():
flash(' Test it flash')
return render_template('test.html')
And then in templates Build a new one inside test.html
<head>text</head>
<body>
{
{get_flashed_messages()}}
</body>
such , This interface is written . And then by visiting localhost:5000/flash You can verify this :
5.2 404,500 Wait for page callback
flash have access to @app.errorhandler( Status code ) To recall which method to call when encountering the corresponding status code , Such as 404,500 etc. .
边栏推荐
- Transfer guide printing system based on C language design
- How to quickly learn a programming language
- P1825 [USACO11OPEN]Corn Maze S
- Which financial product has the highest yield in 2022?
- 高数 | 武爷『经典系列』每日一题思路及易错点总结
- ES6模块化导入导出)(实现页面嵌套)
- Review notes of Microcomputer Principles -- zoufengxing
- pl/sql之集合
- C Entry series (31) -- operator overloading
- Overview of motion recognition evaluation
猜你喜欢
随机推荐
Foundry tutorial: writing scalable smart contracts in various ways (Part 1)
Hegong sky team vision training Day6 - traditional vision, image processing
Database operation skills 6
Which of count (*), count (primary key ID), count (field) and count (1) in MySQL is more efficient? "Suggested collection"
P1825 [USACO11OPEN]Corn Maze S
pl/sql之动态sql与异常
at、crontab
高数 | 武爷『经典系列』每日一题思路及易错点总结
Replication of SQL injection vulnerability in the foreground of Pan micro e-cology8
Day06 homework -- skill question 2
pl/sql之集合
Mutual transformation of array structure and tree structure
PAT 甲级 A1013 Battle Over Cities
One click deployment of lamp and LNMP scripts is worth having
Cadence(十)走线技巧与注意事项
PXE principles and concepts
day06 作业--技能题6
Learn more about the difference between B-tree and b+tree
谷粒学院的全部学习源码
The effective condition of MySQL joint index and the invalid condition of index