home html template and home funciton in library.py

master
grgr 2 years ago
parent 91d74b8b0e
commit 40ebc31f5e

@ -9,6 +9,12 @@ from flask import Flask, render_template, url_for, request, redirect
# ----- functions ----- #
def get_db_connection():
conn = sqlite3.connect('library.db')
conn.row_factory = sqlite3.Row
return conn
# Added row_factory attribute to the sqlite connection, in this way you can have name-based access to columns; this means that the database connection will return rows that behave like regular Python dictionaries.
# ----- FLASK ----- #
@ -37,22 +43,31 @@ app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix='/soupboat/library')
@app.route("/", methods=['GET', 'POST'])
def home():
conn = get_db_connection()
todos = conn.execute('SELECT c.content, cat.title FROM cards c JOIN categories cat \
ON c.category_id = cat.id ORDER BY c.created').fetchall()
categories = {}
if request.method == 'POST':
title = request.form.get('title')
author = request.form.get('author')
description = request.form.get('description')
add_book(author, title, description)
return redirect(url_for('home'))
# for each category and group of cards for each cat in groupby() grouper object
for k, g in groupby(todos, key=lambda t: t['title']):
categories[k] = list(g)
# if request.method == 'POST':
# title = request.form.get('title')
# author = request.form.get('author')
# description = request.form.get('description')
# add_book(author, title, description)
# return redirect(url_for('home'))
return render_template('home.html', reading_list=getAllRows('library.db'))
return render_template('home.html', categories=categories)
@app.route("/add", methods=['GET', 'POST'])
def add_new_page():
def create():
# the goal here is to choose from a list of parameters what you want to add
# 1- ogni blocco del form dovrebbe essere una tabella a parte
if request.method == 'POST':

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>library</title>
<link rel="stylesheet" href="{{url_for('static', filename='style_default.css')}}">
<script src="{{url_for('static', filename='addnew_panel.js')}}"></script>
</head>
<body>
<!-- <a class="nav-link" href="{{ url_for('create') }}">New</a> -->
{% block content %} {% endblock %}
</body>
</html>

@ -1,4 +1,21 @@
<!DOCTYPE html>
{% extends 'base.html' %}
{% block content %}
<h1>{% block title %} Welcome to FlaskTodo {% endblock %}</h1>
{% for category, cards in categories.cards() %}
<div class="card" style="width: 18rem; margin-bottom: 50px;">
<div class="card-header">
<h3>{{ category }}</h3>
</div>
<ul class="list-group list-group-flush">
{% for card in cards %}
<li class="list-group-item">{{ cards['content'] }}</li>
{% endfor %}
</ul>
</div>
{% endfor %}
{% endblock %}
<!-- <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
@ -30,4 +47,4 @@
</div>
</body>
</html>
</html> -->

Loading…
Cancel
Save