Python show-me-the-code No. 0023 topic web Guestbook App

Source: Internet
Author: User
Tags mongoclient

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

Related Article

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.