Use Flask + Python3.6+mysql to write a simple comment module.

Source: Internet
Author: User

Use Flask + Python3.6+mysql to write a simple comment module. After the input is submitted, it appears on the page.

Send and accept data from a comment box

falskbefore returning to the first page of the function, add the methods attribute.

@app.route("/", methods=["GET", "POST"])
Two methods of receiving data are temporarily saved and displayed on the Web page. in-memory storage

The main program is as follows and reload is initialized whenever the program is in process comments .

 fromFlaskImportFlask, redirect, Render_template, request, Url_forapp = Flask (__name__) app.config["DEBUG"] =Truecomments = []@app. Route ("/", methods=["GET", "POST"]) def index():    ifRequest.method = ="GET":returnRender_template ("Main_page.html", comments=comments) Comments.append (request.form["Contents"])returnRedirect (Url_for (' index '))
Persistent write to database Python declaration

First, declare a database management tool statement in Flask SQLAlchemy as follows:

 fromFlask_sqlalchemyImportSqlalchemysqlalchemy_database_uri ="Mysql+mysqlconnector://{username}:{password}@{hostname}/{databasename}". Format (username="Aubucuo", password=' Database password ', hostname="Database Address", databasename="Aubucuo$comments",) app.config["Sqlalchemy_database_uri"] = sqlalchemy_database_uriapp.config["Sqlalchemy_pool_recycle"] =299app.config["Sqlalchemy_track_modifications"] =Falsedb = SQLAlchemy (APP) comments = [] class Comment(db. Model):__tablename__ ="Comments"ID = db. Column (db. Integer, primary_key=True) content = db. Column (db. String (4096))

DB execution app , instantiate link to database.

Command line CREATE table for database
3.6fromimport dbdb.create_all()

After executing the above code, the table is created.

Database commands

The database commands are all at the ; end!

Some common commands about git

git statusView current Code Status
git add filename/文件夹名Update current file
git commit -m ‘说明文字‘Add descriptive text to git for this time
git commit -am "说明文字"Update all the changed files and add this note
git logView the update log
git diffSee where changes are taking place

?

Use Flask + Python3.6+mysql to write a simple comment module.

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.