Construct Dialogue Discussion board with Python, Flask & MySQL
In our earlier Python instructional, we’ve got evolved Online Web Chat using Flask and Python. On this instructional, we can construct a Dialogue Discussion board machine with Python, Flask & MySQL.
A discussion board machine is a program which permits member to carry discussions on-line. The dialogue is began by means of a member by means of posting an issue and different contributors answer on that subject. This permits contributors to percentage knowledge and concepts.
So let’s continue with creating Dialogue Discussion board Machine Challenge:
1. Challenge Setup and Module Set up
First we can create our dialogue discussion board challenge discussion-forum-python-flask-mysql the use of beneath command.
$ mkdir discussion-forum-python-flask-mysql
and moved to the challenge.
$ cd discussion-forum-python-flask-mysql
Then we can set up required modules for our utility. As we can expand a internet primarily based utility, so we can set up Flask micro framework module to create internet utility.
$ pip set up Flask
As we can use MySQL database, so we can additionally set up flask_mysqldb module. That is Python package deal that can be utilized to hook up with MySQL database. We can set up it the use of the beneath command:
pip set up flask_mysqldb
2. Initialize Software
We can create challenge report app.py and import required modules. We can create flask example and likewise configure MySQL database connection.
from flask import Flask, render_template, request, redirect, url_for, consultation, jsonify from flask_mysqldb import MySQL import MySQLdb.cursors from datetime import date import re import os import sys import hashlib app = Flask(__name__) app.secret_key = 'abcd21234455' app.config['MYSQL_HOST'] = 'localhost' app.config['MYSQL_USER'] = 'root' app.config['MYSQL_PASSWORD'] = '' app.config['MYSQL_DB'] = 'boards' mysql = MySQL(app)
3. Put into effect Person Login
We can put in force consumer login capability by means of making a serve as login()
in app.py
. We can show login shape if consumer no longer but login otherise put in force login and redirect to class record web page.
@app.path('/login', strategies =['GET', 'POST']) def login(): mesage="" if request.manner == 'POST' and 'e mail' in request.shape and 'password' in request.shape: e mail = request.shape['email'] #password = hashlib.md5((request.shape['password']).encode('utf-8')) password = hashlib.md5((request.shape['password']).encode('ISO-8859-1 ')).hexdigest() cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor) cursor.execute('SELECT * FROM forum_users WHERE e mail = % s AND password = % s', (e mail, password, )) consumer = cursor.fetchone() if consumer: consultation['loggedin'] = True consultation['userid'] = consumer['user_id'] consultation['name'] = consumer['name'] consultation['email'] = consumer['email'] consultation['role'] = consumer['usergroup'] mesage="Logged in effectively !" go back redirect(url_for('class')) else: mesage="Please input right kind e mail / password !" go back render_template('login.html', mesage = mesage)
We can create template report login.html
for login web page.
{% come with 'header.html' %}{% come with 'top_menus.html' %}