Created
February 20, 2020 19:37
-
-
Save qwadratic/a8dc89e685be8b5d66641cfb29fac3cd to your computer and use it in GitHub Desktop.
Shop App
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Структура проекта: | |
| templates/ | |
| - index.html | |
| createdb.py | |
| run.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title>Shop</title> | |
| </head> | |
| <body> | |
| <h1>My awesome shop admin</h1> | |
| <ul> | |
| {% for name, price in products %} | |
| <li>{{name}} - {{price}} UAH</li> | |
| {% endfor %} | |
| </ul> | |
| <form action="/create-item"> | |
| <input type="text" name="item_name"> | |
| <input type="text" name="item_price"> | |
| <input type="submit"> | |
| </form> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sqlite3 | |
| conn = sqlite3.connect('shop.db') | |
| def create_product_table(): | |
| query = """ | |
| CREATE TABLE product ( | |
| name VARCHAR(30), | |
| price DECIMAL) | |
| """ | |
| conn.execute(query) | |
| conn.commit() | |
| if __name__ == '__main__': | |
| create_product_table() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sqlite3 | |
| from flask import Flask, render_template, request, redirect | |
| app = Flask('shop-app') | |
| def get_all_products(): | |
| query = """ | |
| SELECT name, price | |
| FROM product | |
| """ | |
| conn = sqlite3.connect('shop.db') | |
| cursor = conn.execute(query) | |
| return cursor.fetchall() | |
| @app.route('/') | |
| def index(): | |
| items = get_all_products() | |
| return render_template('index.html', products=items) | |
| def insert_item(item_name, item_price): | |
| query = """ | |
| INSERT INTO product | |
| VALUES (?, ?) | |
| """ | |
| conn = sqlite3.connect('shop.db') | |
| conn.execute(query, (item_name, item_price)) | |
| conn.commit() | |
| @app.route('/create-item') | |
| def create_item(): | |
| name = request.args['item_name'] | |
| price = request.args['item_price'] | |
| insert_item(name, price) | |
| return redirect('/') | |
| app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment