Flask Project Structure (v) using the database

Source: Internet
Author: User

Brief introduction:

The foundation is set up, start reading and writing the database. After all, the process of writing, there is no advanced, is crud, Chinese is added and deleted to change the search.

One: Add test data to the database.

Establish init_test.py in the project root directory

 fromConfigImportbase,db_session fromModels.modelsImport* fromApp.app01.app01_modelsImport*Add_public=Public (Name='Jack_public', email='[email protected]',) Add_private=Private (name='jack_private', email='[email protected]',) Db_session.add (add_public) db_session.add (add_private) db_session.commit () Db_session.remove () Public_user =public.query.filter_by (public_name='Jack_public'). First ()Print(public_user.public_email) Private_user=private.query.filter_by (private_name='jack_private'). First ()Print(Private_user.private_email)

The result of the execution is:
[Email protected]
[Email protected]
Read two e-mail addresses.

II: Use in App/main

Modify/app/main/views.py

 fromFlaskImportBlueprint fromModels.modelsImport*#new addition, introduction of models fromApp.app01.app01_modelsImport*#new addition, introduction of modelsMain= Blueprint ('Main',__name__) @main. Route ('/')defShow (): Public_user= Public.query.filter_by (public_name='Jack_public'). First ()#New Database QueryPrivate_user = private.query.filter_by (private_name='jack_private'). First ()#New Database Querymsg = Public_user.public_email +'<p>'+ Private_user.private_email#Organization return Code    returnMsg#Modify the code returned as just organized
Three: Native test

We only modified the return of the default home page. Home return data as follows

Four: Sync to server

Syncthing Synchronization

V: Create a new Docker image

./rebuild.sh

VI: Test Server Run status

My server and the publishing port are 8800

Seven: Write a test script

The database connection was disconnected after the last deployment.

So write a script for the loop test this time:

ImportREQUESTSR=requests.get ('http://192.168.1.3:8800')Print(R.status_code)ImportTimet2= Time.strftime ('%y-%m-%d%h:%m:%s')Print(T2) I= 1 whileTrue:Try: R=requests.get ('HTTP://192.168.1.3:8800/APP02', timeout=2)        ifR.status_code!=200:            Print(Time.strftime ('%y-%m-%d%h:%m:%s')+'-----------Bed')             Break        Else:            Print(Time.strftime ('%y-%m-%d%h:%m:%s')+'--ok') Time.sleep (1) I+=1except:        Print(Time.strftime ('%y-%m-%d%h:%m:%s') +'-----------Bed')

The code is poorly written and has no record.

It's just a stupid run.

If it's wrong, then stop.

Ten: Omission

Now the routing, static files, permissions are several parts.

Flask Project Structure (v) using the database

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.