Question: what changes need in app.py,update,updaterow so that update can be done on the same row with out creating another row. Add the functionality of 1.
what changes need in app.py,update,updaterow so that update can be done on the same row with out creating another row.
Add the functionality of
1. delete student and
2. update student
to app.py. Expectedly two new .html files will be added to the directory of templates.
app.py
# -*- coding: utf-8 -*- """ Created on Mon Sep 18 09:12:13 2017
@author: chero """
from __future__ import print_function import sys from flask import Flask, request, flash, url_for, redirect, render_template, session, abort from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///students.sqlite3' app.config['SECRET_KEY'] = "random string"
db = SQLAlchemy(app)
@app.route('/') def home(): if not session.get('logged_in'): return render_template('login.html') else: return redirect(url_for('show_all')) @app.route('/login', methods=['POST']) def do_admin_login(): if request.form['password'] == 'password' and request.form['username'] == 'admin': session['logged_in'] = True else: flash('wrong password!') return home()
@app.route("/logout") def logout(): session['logged_in'] = False return home()
class students(db.Model): id = db.Column('student_id', db.Integer, primary_key = True) name = db.Column(db.String(100)) city = db.Column(db.String(50)) addr = db.Column(db.String(200)) pin = db.Column(db.String(10))
def __init__(self, name, city, addr,pin): self.name = name self.city = city self.addr = addr self.pin = pin
class Row(db.Model): id = db.Column('student_id', db.Integer, primary_key = True) name = students.name city = students.city addr = students.addr pin = students.pin
def __init__(self, name, city, addr,pin): self.name = name self.city = city self.addr = addr self.pin = pin
@app.route('/show_all') def show_all(): return render_template('show_all.html', students = students.query.all() )
@app.route('/new', methods = ['GET', 'POST']) def new(): if request.method == 'POST': if not request.form['name'] or not request.form['city'] or not request.form['addr']: flash('Please enter all the fields', 'error') else: student = students(request.form['name'], request.form['city'], request.form['addr'], request.form['pin']) db.session.add(student) db.session.commit() print(student, file=sys.stdout) flash('Record was successfully added') return redirect(url_for('show_all')) return render_template('new.html')
@app.route('/delete', methods = ['GET', 'POST']) def delete():
print ('This is 1') if request.method == 'POST': print ('This is 2')
reID = request.form['reID'] print ('This is 3') student = students.query.get(reID) db.session.delete(student) db.session.commit() flash('Record was successfully removed') return redirect(url_for('show_all'))
return render_template('delete.html', students = students.query.all())
@app.route('/update', methods=['GET', 'POST']) def update(): if request.method == 'POST':
updateID = request.form['updateID']
student1 = students.query.get(updateID) return redirect(url_for('updateRow')) return render_template('update.html', students = students.query.all())
@app.route('/updateRow', methods = ['GET', 'POST']) def updateRow(): if request.method == 'POST': if not request.form['name'] or not request.form['city'] or not request.form['addr']: flash('Please enter all the fields', 'error') else: student = students(request.form['name'], request.form['city'], request.form['addr'], request.form['pin']) db.session.add(student) db.session.commit() flash('Record was successfully added') return redirect(url_for('show_all')) return render_template('updateRow.html')
if __name__ == '__main__': db.create_all() app.run(debug = True)
update
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
Update Student
__________________________________________________
Students in Database
| ID | Name | City | Address | Zip | |
|---|---|---|---|---|---|
| {{ student.id }} | {{ student.name }} | {{ student.city }} | {{ student.addr }} | {{ student.pin }} |
__________________________________________________
_______________________________
Go back to home page
update row
_______________________________
Go back to home page
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
