Basic Information about this - Blog :
In backend I have used python framework - Flask
I have used bootstrap templates .
In this blog we have linked the database with MySql .
In this blog we can add new posts and data will save to database.
In this blog we can edit posts and changes will reflects to database.
In this blog - I have made contact page and all data taken by form will save to database and we will get the email too.
In this blog we can delete post and data will removed from database.
We have osm file-uploader .
We have about page in our blog .
We have login-page too .
We have dashboard page and this will open only after login page .
Code of main.py (backend file )
from flask import Flask, render_template, request, session, redirect from flask.json.tag import PassDict from flask_sqlalchemy import SQLAlchemy from datetime import datetime import pymysql import json from werkzeug.utils import secure_filename from flask_mail import Mail import smtplib import os import math pymysql.install_as_MySQLdb() with open('config.json', 'r') as rd: params = json.load(rd)["pera"] def sendEmail(to, content): server = smtplib.SMTP('smtp.gmail.com', 587) server.ehlo() server.starttls() server.login('rajatdelln@gmail.com', 'shas') server.sendmail('rajadhiman@gmail.com', to, content) server.close() local_server = True app = Flask(__name__) app.secret_key = 'super_secret_key' app.config['UPLOAD_FOLDER'] = params["upload_location"] app.config.update( MAIL_SERVER='smtp.gmail.com', MAIL_PORT='465', MAIL_USE_SSL=True, MAIL_USERNAME=params['gmail_user'], MAIL_PASSWORD=params['gmail_pass'] ) mail = Mail(app) if (local_server): app.config['SQLALCHEMY_DATABASE_URI'] = params['local_url'] else: app.config['SQLALCHEMY_DATABASE_URI'] = params['prod_url'] db = SQLAlchemy(app) class Contact(db.Model): ''' sno, name phone_num, msg, date, email ''' sno = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80), nullable=False) phone_num = db.Column(db.String(12), nullable=False) msg = db.Column(db.String(100), nullable=False) date = db.Column(db.String(12), nullable=True) email = db.Column(db.String(50), nullable=False) class Posts(db.Model): sno = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(80), nullable=False) slug = db.Column(db.String(25), nullable=False) content = db.Column(db.String(200), nullable=False) subhead = db.Column(db.String(25), nullable=False) img_file = db.Column(db.String(25), nullable=True) date = db.Column(db.String(12), nullable=True) @app.route("/") def home(): posts = Posts.query.filter_by().all() last = math.ceil(len(posts)/int(params['num_of_post'])) page = request.args.get('page') if (not str(page).isnumeric()): page = 1 page = int(page) posts = posts[(page-1)*int(params['num_of_post']):(page-1) * int(params['num_of_post']) + int(params['num_of_post'])] if(page == 1): prev = "#" next = "/?page=" + str(page+1) elif page == last: prev = "/?page=" + str(page-1) next = "#" else: prev = "/?page=" + str(page-1) next = "/?page=" + str(page+1) return render_template('index.html', params=params, posts=posts, prev=prev, next=next) @app.route("/logout") def logout(): session.pop('user') return redirect("/dashboard") @app.route("/uploader", methods=['POST', 'GET']) def Uploader(): if ('user' in session and session['user'] == params["admin-user"]): if(request.method == 'POST'): f = request.files['file1'] f.save(os.path.join( app.config['UPLOAD_FOLDER'], secure_filename(f.filename))) return "Uploaded succesfully" @app.route("/post/<string:post_slug>", methods=['GET']) def post_route(post_slug): post = Posts.query.filter_by(slug=post_slug).first() return render_template('post.html', params=params, post=post) @app.route("/dashboard", methods=["GET", "POST"]) def dashboard(): if ('user' in session and session['user'] == params["admin-user"]): posts = Posts.query.all() return render_template('dashboard.html', params=params, posts=posts) if request.method == 'POST': username = request.form.get('uname') userpass = request.form.get('pass') if (username == params["admin-user"] and userpass == params["admin-pass"]): session['user'] = username posts = Posts.query.all() return render_template('dashboard.html', params=params, posts=posts) return render_template('login.html', params=params) @app.route("/edit/<string:sno>", methods=['GET', 'POST']) def edit(sno): if ('user' in session and session['user'] == params["admin-user"]): if(request.method == 'POST'): box_title = request.form.get('title') subheading = request.form.get('subhead') slug = request.form.get('slug') content = request.form.get('content') img_file = request.form.get('img_file') date = datetime.now() if (sno == '0'): post = Posts(title=box_title, subhead=subheading, slug=slug, content=content, img_file=img_file, date=date) db.session.add(post) db.session.commit() else: post = Posts.query.filter_by(sno=sno).first() post.title = box_title post.subhead = subheading post.slug = slug post.content = content post.img_file = img_file post.date = date db.session.commit() return redirect('/edit/'+sno) post = Posts.query.filter_by(sno=sno).first() return render_template('edit.html', params=params, post=post, sno=sno) @app.route("/login") def login_new(): # if ('user' in session and session['user'] == params["admin-user"]): return render_template('login.html', params=params) @app.route("/about") def about(): return render_template('about.html') @app.route("/contact", methods=['GET', 'POST']) def contact(): if(request.method == 'POST'): '''Add entry to the database''' names = request.form.get('names') email = request.form.get('email') phone = request.form.get('phone') message = request.form.get('message') entry = Contact(name=names, phone_num=phone, msg=message, date=datetime.now(), email=email) db.session.add(entry) db.session.commit() datamsg = f" new msg is snd by{names} ans email is {email} ans msg is {message} and datetime is {datetime}" # emailnam = "rajat.dhiman.3000@gmail.com" # sendEmail(emailnam, datamsg) # # mail.send_message( # "new message from " + names, # sender = email, # recipients = [params["gmail_user"]], # body = message + "\n"+ phone # ) return render_template('contact.html') @app.route('/delete/<int:post_id>') def delete(post_id): post = Posts.query.get(post_id) db.session.delete(post) db.session.commit() return redirect('/dashboard') app.run(debug=True)
Download link to Blog Folder
This folder contains static folder in which we put css , font , js , images files .
This folder also contains templates folder in which we have many templates such post.html , login.html , about.html etc .
Comments