form but it doesn't work

master
grgr 2 years ago
parent 2b632ff905
commit c5594f6da0

@ -5,7 +5,7 @@ from itertools import groupby # to handle complex iterations
import os import os
import sqlite3 import sqlite3
from flask import Flask, render_template, url_for, request, redirect from flask import Flask, render_template, url_for, request, redirect, flash
# ----- functions ----- # # ----- functions ----- #
@ -72,9 +72,25 @@ def home():
def create(): def create():
conn = get_db_connection() conn = get_db_connection()
if request.method == 'POST':
content = request.form['content']
cat_title = request.form['cat']
if not content:
flash('plz write a content!')
return redirect(url_for('home'))
cat_id = conn.execute('SELECT id FROM categories WHERE title = (?);',
(cat_title,)).fetchone()['id']
conn.execute('INSERT INTO cards (content, category_id) VALUES (?,?)',
(content, cat_title))
conn.commit()
conn.close()
return redirect(url_for('home'))
categories = conn.execute('SELECT title FROM categories;').fetchall() categories = conn.execute('SELECT title FROM categories;').fetchall()
conn.close() conn.close()
return render_template('create.html', categories=categories) return render_template('create.html', categories=categories)

@ -7,7 +7,19 @@
<!-- <script src="{{url_for('static', filename='addnew_panel.js')}}"></script> --> <!-- <script src="{{url_for('static', filename='addnew_panel.js')}}"></script> -->
</head> </head>
<body> <body>
<a class="nav-link" href="{{ url_for('create') }}">New</a> <ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="{{ url_for('create') }}">New</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="#">About</a>
</li>
</ul>
{% for message in get_flashed_messages() %}
<div class="alert alert-danger">{{ message }}</div>
{% endfor %}
{% block content %} {% endblock %} {% block content %} {% endblock %}
</body> </body>
</html> </html>

@ -1,11 +1,19 @@
<!DOCTYPE html> {% extends 'base.html' %}
<html lang="en">
<head> {% block content %}
<meta charset="utf-8" /> <h1>{% block title %} Create a New Item {% endblock %}</h1>
<title>library</title>
</head> <form method="post">
<body> <div class="form-group">
<select class="form-control" name="cat"> <label for="content">Content</label>
<input type="text" name="content"
placeholder="Todo content" class="form-control"
value="{{ request.form['content'] }}"></input>
</div>
<div class="form-group">
<label for="category">category/type</label>
<select class="form-control" name="category">
{% for cat in categories %} {% for cat in categories %}
{% if cat['title'] == request.form['cat'] %} {% if cat['title'] == request.form['cat'] %}
<option value="{{ request.form['cat'] }}" selected> <option value="{{ request.form['cat'] }}" selected>
@ -18,6 +26,10 @@
{% endif %} {% endif %}
{% endfor %} {% endfor %}
</select> </select>
<div id="panel-container"></div> </div>
</body> <div class="form-group">
</html> <button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
{% endblock %}

Loading…
Cancel
Save