Python-flask Framework uses Flask_mongoengine

Source: Internet
Author: User

Development environment Configuration

Before using MongoDB, you need to install Pymongo, and Flask_mongoengine

1. Switch to the VIRTUALENV environment    ./pyenv/bin/activate    2. Install Pymongo    pip install Pymongo3. Install Flask_mongoengine    pip Install Flask_mongoengine

  

Writing model

models.py, the code is as follows

From Flask_mongoengine import mongoengine#--#进行配置app. config[' mongodb_settings '] = {    ' db '    : ' The_way_to_flask ',    ' host '  : ' localhost ',    ' Port '  : 27017} # Create MONGO prototype MDB = Mongoengine () Mdb.init_app (APP) class Address (MDB. Document):    name = mdb. Stringfield ()    address = mdb. Stringfield ()        # query Address.objects (name= "Zhangsan"). First ()    # Add Address (name= ' Lisi ', address= ' [email Protected]. Save ()    # Delete Address.delete ()    # update address.update (name= "[email protected]")

  

Writing a View

views.py, the code is as follows

From models import addressfrom flask import jsonify#--@app. Route ('/mdb_list ', methods=[' get ') def mdb_list ():    name = Request.args.get (' name ')    address = request.args.get (' address ')    Addr = address.objects (name=name,address= Address). First ()    if not Addr:        address (Name=name, address=address). Save ()        return jsonify ({' Code ': 1, ' Message ': ' Success '} '    else:        return jsonify (Addr.to_json ())

  

Python-flask Framework uses Flask_mongoengine

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.