利用Flask + python3.6+MYSQL編寫一個簡單的評論模組。

來源:互聯網
上載者:User

標籤:lan   cbo   pbm   env   fnr   ref   檔案   body   hbm   

利用flask + python3.6+MYSQL編寫一個簡單的評論模組。輸入內容提交後,會顯示在頁面上。

發送並接受評論框的資料

falsk返回首頁的函數前,加入methods屬性。

@app.route("/", methods=["GET", "POST"])
兩種接收資料的方式暫時性的儲存,並顯示在網頁上. in-memory storage

主程式如下,每當程式reload時候,comments會被初始化。

from flask import Flask, redirect, render_template, request, url_forapp = Flask(__name__)app.config["DEBUG"] = Truecomments = []@app.route("/", methods=["GET", "POST"])def index():    if request.method == "GET":        return render_template("main_page.html", comments=comments)    comments.append(request.form["contents"])    return redirect(url_for(‘index‘))
永久性的寫入資料庫Python裡聲明

首先聲明一個Flask裡的資料庫管理工具SQLAlchemy語句如下:

from flask_sqlalchemy import SQLAlchemySQLALCHEMY_DATABASE_URI = "mysql+mysqlconnector://{username}:{password}@{hostname}/{databasename}".format(    username="aubucuo",    password=‘資料庫密碼‘,    hostname="資料庫地址",    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執行app,執行個體化連結到資料庫。

命令列建立資料庫的table
ipython 3.6from flask_app import dbdb.create_all()

執行完以上代碼,table就被建立了。

資料庫命令

資料庫的命令都是以;結尾的!

關於git的一些常用指令

git status查看當前代碼狀態
git add filename/檔案夾名更新當前檔案
git commit -m ‘解說文字‘為本次git 添加解說文字
git commit -am "解說文字"更新所有變更的檔案並添加這個說明
git log查看更新日誌
git diff查看發生變化的地方

?

利用Flask + python3.6+MYSQL編寫一個簡單的評論模組。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.