私はSQLite3データベースにStudents
テーブルを持っています。このテーブルには、IDフィールドと名前フィールドがあります。Flask - db.session.delete()を使用してDB内のレコードを削除
ここに私のapp.pyだ:私はちょうど今、私が削除したい、私はレコードを追加する場所から、私はレコードを削除していた場所にはなく名前フェイルドなしのテンプレートをコピーし
#!/usr/bin/python
from flask import Flask, render_template, json, request, flash,session,redirect,url_for
from flask_sqlalchemy import SQLAlchemy
import sqlite3
conn = sqlite3.connect('test.db')
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///students.sqlite3'
db = SQLAlchemy(app)
class Students(db.Model):
id = db.Column('student_id',db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True)
def __init__(self, name, id):
self.name = name
self.id = id
def __repr__(self):
return '<Students %r>' % self.name
db.create_all()
@app.route("/")
def main():
return render_template('index.html')
@app.route('/new', methods = ['GET', 'POST'])
def new():
if request.method == 'POST':
if not request.form['name'] or not request.form['id']:
flash('Please enter all the fields', 'error')
else:
student = Students(request.form['name'], request.form['id'])
db.session.add(student)
db.session.commit()
flash('Record was successfully added')
return redirect(url_for('show_all'))
return render_template('new.html')
@app.route('/deleted', methods = ['GET', 'POST'])
def deleted():
if request.method == 'POST':
if not request.form['name'] or not request.form['id']:
flash('Please enter the right number you asshole!','error')
else:
student1 = Students(request.form['id'])
Students.query.filter_by(id='student1').delete()
db.session.commit()
flash('Record was deleted successfully ')
return redirect(url_for('show_all'))
return render_template('deleted.html')
@app.route("/admin")
def show_all():
return render_template('show_all.html', Students = Students.query.all())
IDだけで記録する。私はIDフォームから値を要求した後、私はそれを削除するために、クエリにそれを渡された変数students1
にそれを割り当てられたこのよう
User.query.filter_by(id=123).delete()
:私は、スタックオーバーフローの前の質問からこれを得ました。 show_allテンプレートをリダイレクトして再配置しました。したがって
、この作り:
@app.route('/deleted', methods = ['GET', 'POST'])
def deleted():
if request.method == 'POST':
if not request.form['name'] or not request.form['id']:
flash('Please enter the right number you asshole!','error')
else:
student1 = Students(request.form['id'])
Students.query.filter_by(id='student1').delete()
db.session.commit()
flash('Record was deleted successfully ')
return redirect(url_for('show_all'))
return render_template('deleted.html')
をしかし、私は400不正な要求エラーを取得しておきます。どんな助けもありがとうございます。私はデータベースの学習を始めました。
deleted.html私はこれがあなたのレコードを削除した非常にわからない
<!DOCTYPE html>
<html>
<body>
<h3>SCS EC Attendance Kickout the student</h3>
<hr/>
{%- for category, message in get_flashed_messages(with_categories = true) %}
<div class = "alert alert-danger">
{{ message }}
</div>
{%- endfor %}
<form action = "{{ request.path }}" method = "post">
<label for = "id">Id Number</label><br>
<input type = "text" name = "id" placeholder = "Student Id" /><br>
<input type = "submit" value = "Delete this motherfucker!" />
</form>
</body>
</html>
show_all.html
<!DOCTYPE html>
<html lang = "en">
<head>
<link rel="stylesheet" type="text/css" href="table.css">
</head>
<body>
<h3>
<a href = "{{ url_for('show_all') }}">SCS EC Attendance System</a>
</h3>
<hr/>
{%- for message in get_flashed_messages() %}
{{ message }}
{%- endfor %}
<h3>Students (<a href = "{{ url_for('new') }}">Add Student
</a>)</h3>
<h3>Students (<a href = "{{ url_for('deleted') }}">Massacre Student
</a>)</h3>
<table class="responstable">
<thead>
<tr>
<th>Name</th>
<th>Id</th>
</tr>
</thead>
<tbody>
{% for Student in Students %}
<tr>
<td>{{ Student.name }}</td>
<td>{{ Student.id }}</td>
{% endfor %}
</tbody>
</table>
<p class="lead"></p>
<p><a class="btn btn-lg btn-success" href="new" role="button">Sign up today</a>
<a class="btn btn-lg btn-success" href="show_all" role="button">Administrate</a>
</p>
</body>
</html>
dbをアプリケーションに添付しましたか?あなたのコードに 'db.init_app(app)'は表示されません。 – nos
私はちょうどそれを追加しました、ありがとう!しかし、それは本当に問題を解決しませんでした。 :( –