How to connect mongodb with python flask and perform save/get operations

from flask import Flask, render_template, request, jsonify, redirect, url_for
from pymongo import MongoClient
from bson.json_util import dumps

client = MongoClient('mongodb://1.2.3.4:27017/')
ngdev = client.posts //posts is db here/

@app.route('/save', methods=['POST'])
def save():
        request_data = request.get_json()
        try:
                client.ngdev.program.insert_one(request_data)
        except err:
                return "Mongodb insertion failed"
        return "success"


@app.route('/posts', methods=['GET'])
def getPosts():
        programs = dumps(client.ngdev.posts.find({},{"tags":0,"_id":0,"image":0}))        
        return  programs

if __name__ == '__main__':
        app.run(host='0.0.0.0',port=4999)

3 comments

  • Been playing on pk07 for a while now. Nothing spectacular but it gets the job done when I’m bored. It’s cool that you can play anywhere.Check pk07 out just for a quick and easy experience.

  • Really impressed with the bonuses they offer for new players. It feels like they actually value the users. Love playing at 119betwin.

  • I have tested quite a few apps over the years and this one keeps me coming back. The slot games load instantly and I got my first withdrawal processed within a few hours. It really shows how much effort they put into the user experience. tk222app

Leave a Reply