Untitled

 avatar
unknown
python
3 years ago
5.6 kB
4
Indexable
from operator import imod
from django.shortcuts import redirect
from flask import Flask
from flask import render_template
from requests import request, session
from flask_wtf import FlaskForm
from wtFroms import Stringfield
from wtforms.validators import DataRequired
from flask_sqlalchemy import SQLAlchemy
import pymysql
import secrets


conn = "mysql+pymysql://{0}:{1}@{2}/{3}".format(secrets.dbuser, secrets.dbpass, secrets.dbhost, secrets.dbname)

app = Flask(__name__)
app.config["SECRET_KEY"]='SecretKey'
app.config["SQLALCHEMY_DATABASE_URI"] = conn
db = SQLAlchemy(app)

class Shopping_list(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    Shopping_list = db.Column(db.String(255))


def __repr__(self):
    Shopping_list = StringField("Shopping List", validators=[DataRequired()]
    

@app.route("/")
def index():
    return render_template("index.html", pageTitle="Buyer\'s Shopping List")

@app.route("/Shopping_List", methods=["GET", "POST"])
def add_shopping_list():
    form = Shopping_listForm():
    if form,validate_on_submit():
        Shopping_list = Shopping_list(Shopping_list=form.Shopping_list.data)
        db.session.add(Shopping_list)
        db.session.commit()
        return "My shopping list is {0}".format(form.Shopping_list.data)
    return render_template("add_shopping_list.html", form=form, pageTitle="Add a new shopping list")



@app.route("/delete_shopping_list/<int:shopping_list_id>", methods=["GET", "POST"])
def delete_shopping_list():
    if request.method == "POST":
        Shopping_list = Shopping_list.query.get_or_4040(Shopping_list_id)
        db.session.delete(Shopping_list)
        db.session.commit()
        return redirect("/")
    else:
        return redirect("/")







if __name__ == "__main__":
    app.run(debug=True)








# from flask import Flask, render_template, request, redirect, url_for
# from flask_sqlalchemy import SQLAlchemy 

# app = Flask(__name__)

# app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////mnt/C:/Users/DT User6/Desktop/pl/sa bazom/todo.db'

# db = SQLAlchemy(app)

# class ShoppingList(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     text = db.Column(db.String(200))
#     complete = db.Column(db.Boolean)

# @app.route('/')
# def index():
#     incomplete = ShoppingList.query.filter_by(complete=False).all()
#     complete = ShoppingList.query.filter_by(complete=True).all()

#     return render_template('index.html', incomplete=incomplete, complete=complete)

# @app.route('/add', methods=['POST'])
# def add():
#     todo = ShoppingList(text=request.form['todoitem'], complete=False)
#     db.session.add(todo)
#     db.session.commit()

#     return redirect(url_for('index'))

# @app.route('/complete/<id>')
# def complete(id):

#     todo = ShoppingList.query.filter_by(id=int(id)).first()
#     todo.complete = True
#     db.session.commit()
    
#     return redirect(url_for('index'))

# if __name__ == '__main__':
#     app.run(debug=True)














# from flask import Flask, render_template, request, redirect, url_for, flash
# from flask_sqlalchemy import SQLAlchemy
 
 
 
 
# app = Flask(__name__)
# app.secret_key = "Secret Key"
 
# #SqlAlchemy Database Configuration With Mysql
# app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:''@localhost/crud'
# app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
 
# db = SQLAlchemy(app)
 
 
# #Creating model table for our CRUD database
# class Data(db.Model):
#     id = db.Column(db.Integer, primary_key = True)
#     name = db.Column(db.String(100))
#     email = db.Column(db.String(100))
#     phone = db.Column(db.String(100))
 
 
#     def __init__(self, name, email, phone):
 
#         self.name = name
#         self.email = email
#         self.phone = phone
 
 
 
 
 
# #This is the index route where we are going to
# #query on all our employee data
# @app.route('/')
# def Index():
#     all_data = Data.query.all()
 
#     return render_template("index.html", employees = all_data)
 
 
 
# #this route is for inserting data to mysql database via html forms
# @app.route('/insert', methods = ['POST'])
# def insert():
 
#     if request.method == 'POST':
 
#         name = request.form['name']
#         email = request.form['email']
#         phone = request.form['phone']
 
 
#         my_data = Data(name, email, phone)
#         db.session.add(my_data)
#         db.session.commit()
 
#         flash("Employee Inserted Successfully")
 
#         return redirect(url_for('Index'))
 
 
# #this is our update route where we are going to update our employee
# @app.route('/update', methods = ['GET', 'POST'])
# def update():
 
#     if request.method == 'POST':
#         my_data = Data.query.get(request.form.get('id'))
 
#         my_data.name = request.form['name']
#         my_data.email = request.form['email']
#         my_data.phone = request.form['phone']
 
#         db.session.commit()
#         flash("Employee Updated Successfully")
 
#         return redirect(url_for('Index'))
 
 
 
 
# #This route is for deleting our employee
# @app.route('/delete/<id>/', methods = ['GET', 'POST'])
# def delete(id):
#     my_data = Data.query.get(id)
#     db.session.delete(my_data)
#     db.session.commit()
#     flash("Employee Deleted Successfully")
 
#     return redirect(url_for('Index'))
 
 
 
 
 
 
# if __name__ == "__main__":
#     app.run(debug=True)
Editor is loading...