├── Dockerfile ├── README.md ├── app.py ├── data.pyc └── templates ├── about.html ├── add_article.html ├── article.html ├── articles.html ├── dashboard.html ├── edit_article.html ├── home.html ├── includes ├── _formhelpers.html ├── _messages.html └── _navbar.html ├── layout.html ├── login.html └── register.html /Dockerfile: -------------------------------------------------------------------------------- 1 | Hello 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Flask-Webapplication-with-mysql 2 | Python Flask Web application with mysql database 3 | If you need any help Reach me at ## vasanth@pinesphere.com 4 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | from flask import Flask,render_template, flash, redirect , url_for , session ,request, logging 2 | from flask_mysqldb import MySQL 3 | from wtforms import Form, StringField , TextAreaField ,PasswordField , validators 4 | from passlib.hash import sha256_crypt 5 | from functools import wraps 6 | 7 | 8 | app = Flask(__name__) 9 | app.debug = True 10 | 11 | 12 | #Config MySQL 13 | app.config['MYSQL_HOST'] = 'localhost' 14 | app.config['MYSQL_USER'] = 'root' 15 | app.config['MYSQL_PASSWORD'] = 'root' 16 | app.config['MYSQL_DB'] = 'myflaskapp' 17 | app.config['MYSQL_CURSORCLASS'] = 'DictCursor' 18 | #init MYSQL 19 | mysql = MySQL(app) 20 | 21 | 22 | #Articles = Articles() 23 | 24 | @app.route('/') 25 | def index(): 26 | return render_template('home.html') 27 | 28 | @app.route('/about') 29 | def about(): 30 | return render_template('about.html') 31 | 32 | @app.route('/articles') 33 | def articles(): 34 | 35 | #create cursor 36 | cur = mysql.connection.cursor() 37 | 38 | #get articles 39 | result = cur.execute("SELECT * FROM articles") 40 | 41 | articles = cur.fetchall() 42 | 43 | if result > 0: 44 | return render_template('articles.html',articles=articles) 45 | else: 46 | msg = 'No Articles Found' 47 | return render_template('articles.html',msg=msg) 48 | #close connection 49 | cur.close() 50 | 51 | 52 | 53 | @app.route('/article//') 54 | def article(id): 55 | #create cursor 56 | cur = mysql.connection.cursor() 57 | 58 | #get article 59 | result = cur.execute("SELECT * FROM articles WHERE id = %s",[id]) 60 | 61 | article = cur.fetchone() 62 | 63 | return render_template('article.html',article=article) 64 | 65 | class RegisterForm(Form): 66 | name = StringField('Name',[validators.Length(min=1,max=50)]) 67 | username = StringField('Username',[validators.Length(min=4,max=25)]) 68 | email = StringField('Email',[validators.Length(min=4,max=25)]) 69 | password = PasswordField('Password', [ validators.DataRequired (),validators.EqualTo('confirm',message ='passwords do not match')]) 70 | confirm = PasswordField('Confirm password') 71 | 72 | @app.route('/register', methods=['GET','POST']) 73 | def register(): 74 | form = RegisterForm(request.form) 75 | if request.method == 'POST' and form.validate(): 76 | name = form.name.data 77 | email = form.email.data 78 | username = form.username.data 79 | password = sha256_crypt.encrypt(str(form.password.data)) 80 | 81 | # Create crusor 82 | cur = mysql.connection.cursor() 83 | 84 | cur.execute("INSERT INTO users(name,email,username,password) VALUES(%s,%s,%s,%s)",(name,email,username,password)) 85 | 86 | # commit to DB 87 | mysql.connection.commit() 88 | #close connection 89 | cur.close() 90 | 91 | flash("You are now Registered and you can login" , 'success') 92 | 93 | redirect(url_for('login')) 94 | return render_template('register.html',form=form) 95 | 96 | # user login 97 | @app.route('/login',methods =['GET','POST']) 98 | def login(): 99 | if request.method == 'POST': 100 | #Get Form Fields 101 | username = request.form['username'] 102 | password_candidate = request.form['password'] 103 | 104 | # Create cursor 105 | 106 | cur = mysql.connection.cursor() 107 | 108 | #Get user by username 109 | 110 | result = cur.execute("SELECT * FROM users WHERE username = %s" ,[username]) 111 | 112 | if result > 0: 113 | # Get Stored hash 114 | data = cur.fetchone() 115 | password = data['password'] 116 | 117 | # Compare Passwords 118 | if sha256_crypt.verify(password_candidate,password): 119 | #Passed 120 | session['logged_in'] = True 121 | session['username'] = username 122 | 123 | flash('You are now logged in ','success') 124 | return redirect(url_for('dashboard')) 125 | else: 126 | error = 'Username not found' 127 | return render_template('login.html',error=error) 128 | #close connection 129 | cur.close() 130 | 131 | else: 132 | error = 'Username not found' 133 | return render_template('login.html',error=error) 134 | 135 | return render_template('login.html') 136 | 137 | #check if user logged in 138 | 139 | def is_logged_in(f): 140 | @wraps(f) 141 | def wrap(*args,**kwargs): 142 | if 'logged_in' in session: 143 | return f(*args, **kwargs) 144 | else: 145 | flash('Unauthorized, please login','danger') 146 | return redirect(url_for('login')) 147 | return wrap 148 | 149 | 150 | 151 | #logout 152 | @app.route('/logout') 153 | @is_logged_in 154 | def logout(): 155 | session.clear() 156 | flash('you are now logged out ','success') 157 | return redirect(url_for('login')) 158 | # Dashboard 159 | @app.route('/dashboard') 160 | @is_logged_in 161 | def dashboard(): 162 | 163 | #create cursor 164 | cur = mysql.connection.cursor() 165 | 166 | #get articles 167 | result = cur.execute("SELECT * FROM articles") 168 | 169 | articles = cur.fetchall() 170 | 171 | if result > 0: 172 | return render_template('dashboard.html',articles=articles) 173 | else: 174 | msg = 'No Articles Found' 175 | return render_template('dashboard.html',msg=msg) 176 | #close connection 177 | cur.close() 178 | 179 | #Article form class 180 | 181 | class ArticleForm(Form): 182 | title = StringField('Title',[validators.Length(min=1,max=50)]) 183 | body = TextAreaField('Body',[validators.Length(min=30,max=1000)]) 184 | 185 | #Add Article 186 | 187 | @app.route('/add_article', methods=['GET','POST']) 188 | @is_logged_in 189 | def add_article(): 190 | form = ArticleForm(request.form) 191 | if request.method == 'POST' and form.validate(): 192 | title = form.title.data 193 | body = form.body.data 194 | 195 | # Create a cursor 196 | 197 | cur = mysql.connection.cursor() 198 | 199 | #execute 200 | 201 | cur.execute("INSERT INTO articles(title,body,author) VALUES(%s, %s, %s)",(title, body, session['username'])) 202 | 203 | #commit to db 204 | 205 | mysql.connection.commit() 206 | 207 | #close connection 208 | cur.close() 209 | 210 | flash('Article created ','success') 211 | 212 | return redirect(url_for('dashboard')) 213 | 214 | return render_template('add_article.html',form=form) 215 | 216 | #Edit Article 217 | 218 | @app.route('/edit_article/', methods=['GET','POST']) 219 | @is_logged_in 220 | def edit_article(id): 221 | # Create cursor 222 | cur = mysql.connection.cursor() 223 | #get article by id 224 | result = cur.execute("SELECT * FROM articles WHERE id = %s", [id]) 225 | 226 | article = cur.fetchone() 227 | 228 | #get form 229 | form = ArticleForm(request.form) 230 | 231 | #populate article form fields 232 | form.title.data = article['title'] 233 | form.body. data = article['body'] 234 | 235 | if request.method == 'POST' and form.validate(): 236 | title = request.form['title'] 237 | body = request.form['body'] 238 | 239 | # Create a cursor 240 | 241 | cur = mysql.connection.cursor() 242 | 243 | #execute 244 | 245 | cur.execute("UPDATE articles SET title=%s, body=%s WHERE id = %s" , (title,body,id)) 246 | 247 | #commit to db 248 | 249 | mysql.connection.commit() 250 | 251 | #close connection 252 | cur.close() 253 | 254 | flash('Article Updated ','success') 255 | 256 | return redirect(url_for('dashboard')) 257 | 258 | return render_template('edit_article.html',form=form) 259 | 260 | #Delete article 261 | @app.route('/delete_article/', methods=['POST']) 262 | @is_logged_in 263 | def delete_article(id): 264 | # Create cursor 265 | cur = mysql.connection.cursor() 266 | 267 | #Execute 268 | cur.execute("DELETE FROM articles WHERE id = %s",[id]) 269 | 270 | #Commit to DB 271 | 272 | mysql.connection.commit() 273 | #close connection 274 | 275 | cur.close() 276 | 277 | flash('Article Deleted ','success') 278 | 279 | return redirect(url_for('dashboard')) 280 | 281 | 282 | 283 | if __name__ =='__main__': 284 | app.secret_key='secret123' 285 | app.run() 286 | -------------------------------------------------------------------------------- /data.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itzvnl/Flask-Webapplication-with-mysql/a51a8ae2c3304b823d320944ef3fbd90603e58fa/data.pyc -------------------------------------------------------------------------------- /templates/about.html: -------------------------------------------------------------------------------- 1 | {% extends 'layout.html' %} 2 | 3 | {% block body %} 4 |

About US

5 |

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

6 | {% endblock %} 7 | -------------------------------------------------------------------------------- /templates/add_article.html: -------------------------------------------------------------------------------- 1 | {% extends 'layout.html' %} 2 | 3 | {% block body %} 4 |

Add Articles

5 | {% from "includes/_formhelpers.html" import render_field %} 6 |
7 |
8 | {{ render_field(form.title,class_="form-control") }} 9 |
10 |
11 | {{ render_field(form.body, class_="form-control",id="editor") }} 12 |
13 |

14 |

15 | {% endblock %} 16 | -------------------------------------------------------------------------------- /templates/article.html: -------------------------------------------------------------------------------- 1 | {% extends 'layout.html' %} 2 | 3 | {% block body %} 4 |

{{article.title}}

5 | written by {{article.author}} on {{article.create_date}} 6 |
7 |
8 | {{article.body | safe }} 9 |
10 | {% endblock %} 11 | -------------------------------------------------------------------------------- /templates/articles.html: -------------------------------------------------------------------------------- 1 | {% extends 'layout.html' %} 2 | 3 | {% block body %} 4 |

Articles

5 | 10 | {% endblock %} 11 | -------------------------------------------------------------------------------- /templates/dashboard.html: -------------------------------------------------------------------------------- 1 | {% extends 'layout.html' %} 2 | 3 | {% block body %} 4 |

DashboardWelcome {{session.username}}

5 | Add Articles 6 |
7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | {% for article in articles %} 17 | 18 | 19 | 20 | 21 | 22 | 23 | 29 | 30 | {% endfor %} 31 |
IDTitleAuthorDate
{{article.id}}{{article.title}}{{article.author}}{{article.create_date}}Edit 24 |
25 | 26 | 27 |
28 |
32 | {% endblock %} 33 | -------------------------------------------------------------------------------- /templates/edit_article.html: -------------------------------------------------------------------------------- 1 | {% extends 'layout.html' %} 2 | 3 | {% block body %} 4 |

Edit Article

5 | {% from "includes/_formhelpers.html" import render_field %} 6 |
7 |
8 | {{ render_field(form.title,class_="form-control") }} 9 |
10 |
11 | {{ render_field(form.body, class_="form-control",id="editor") }} 12 |
13 |

14 |

15 | {% endblock %} 16 | -------------------------------------------------------------------------------- /templates/home.html: -------------------------------------------------------------------------------- 1 | {% extends 'layout.html' %} 2 | 3 | {% block body %} 4 |
5 |

Vasanth Python Tutorials

6 |

This Application is build using python and flask framework

7 | {% if session.logged_in == NULL %} 8 | Register 9 | Login 10 | {% endif %} 11 |
12 | {% endblock %} 13 | -------------------------------------------------------------------------------- /templates/includes/_formhelpers.html: -------------------------------------------------------------------------------- 1 | {% macro render_field(field) %} 2 | {{ field.label }} 3 | {{ field(**kwargs)|safe }} 4 | {% if field.errors %} 5 | {% for error in field.errors %} 6 | {{ error }} 7 | {% endfor %} 8 | {% endif %} 9 | {% endmacro %} 10 | -------------------------------------------------------------------------------- /templates/includes/_messages.html: -------------------------------------------------------------------------------- 1 | {% with messages = get_flashed_messages(with_categories=true) %} 2 | {% if messages %} 3 | {% for category, message in messages %} 4 |
{{ message }}>
5 | {% endfor %} 6 | {% endif %} 7 | {% endwith %} 8 | 9 | 10 | {% if error %} 11 |
{{error}}
12 | {% endif %} 13 | 14 | 15 | {% if msg %} 16 |
{{msg}}
17 | {% endif %} 18 | -------------------------------------------------------------------------------- /templates/includes/_navbar.html: -------------------------------------------------------------------------------- 1 | 32 | -------------------------------------------------------------------------------- /templates/layout.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Vasanth Flask App 4 | 5 | 6 | 7 | {% include 'includes/_navbar.html'%} 8 | 9 |
10 | {% include 'includes/_messages.html' %} 11 | {% block body %}{% endblock %} 12 |
13 | 14 | 15 | 16 | 17 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /templates/login.html: -------------------------------------------------------------------------------- 1 | {% extends 'layout.html' %} 2 | 3 | {% block body %} 4 |

Login

5 | 6 |
7 |
8 | 9 | 10 |
11 |
12 | 13 | 14 |
15 | 16 |
17 | {% endblock %} 18 | -------------------------------------------------------------------------------- /templates/register.html: -------------------------------------------------------------------------------- 1 | {% extends 'layout.html' %} 2 | 3 | {% block body %} 4 |

Register

5 | {% from "includes/_formhelpers.html" import render_field%} 6 |
7 |
8 | {{render_field(form.name,class="form-control")}} 9 |
10 |
11 | {{render_field(form.email,class="form-control")}} 12 |
13 |
14 | {{render_field(form.username,class="form-control")}} 15 |
16 |
17 | {{render_field(form.password,class="form-control")}} 18 |
19 |
20 | {{render_field(form.confirm,class="form-control")}} 21 |
22 |

23 |
24 | {% endblock %} 25 | --------------------------------------------------------------------------------