question No. 0023: Use the Python web framework to make a web version of the Guestbook app.
Reading material: What WEB frameworks are available in Python
Ideas:
Flask is a lightweight WEB application framework written using Python.
This app uses flask in the background, database with MongoDB. Before using Tornado to write similar, so try to use the flask. Write styles and templates on the front end and return data rendering from the back end. The message is submitted to the backend and then stored using MongoDB. Because this application is relatively simple, flask this lightweight framework and MongoDB is appropriate and easy to use.
Code:
Only the py file is given here.
Full code see: GitHub Address
run.py
fromFlaskImportFlask, request, Render_template, url_forImportPymongoImportDatetimeapp = Flask (__name__)# Database SettingMongoclient = Pymongo. Mongoclient (' localhost ',27017) db = mongoclient[' Msgboard ']# conn = Pymongo. Connection (' localhost ', 27017)# Msgboard = Conn.msgboard# msgboard.create_collection (' msg ')@app. Route ('/', methods=[' GET ', ' POST ') def index(): ifRequest.method = =' GET ':returnRender_template (' index.html ', msglist=db[' msg '].find ())Else: Msg_collection = db[' msg '] Time = Datetime.datetime.now (). Strftime ('%y/%m/%d%h:%m:%s ') Msg_collection.insert ({' msg ': request.form[' msg '],' Time ': Time})returnRender_template (' index.html ', msglist=db[' msg '].find ())if__name__ = =' __main__ ': App.run (debug=True)
Operating effect:
Python show-me-the-code No. 0023 topic web Guestbook App