- version : 0.2
- description : add full info for every student .
هذا الالتزام موجود في:
48
app.py
48
app.py
@@ -18,15 +18,25 @@ def init_db():
|
||||
conn = get_db_connection()
|
||||
# Use a 'with' statement to automatically handle closing the connection
|
||||
with conn:
|
||||
# Drop the table if it exists to ensure the new schema is applied
|
||||
conn.execute('DROP TABLE IF EXISTS students')
|
||||
# Create the table with the new, expanded schema
|
||||
conn.execute('''
|
||||
CREATE TABLE IF NOT EXISTS students (
|
||||
CREATE TABLE students (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
student_name TEXT NOT NULL,
|
||||
age INTEGER NOT NULL,
|
||||
parent_name TEXT NOT NULL,
|
||||
phone_number TEXT NOT NULL
|
||||
parent_phone_1 TEXT NOT NULL,
|
||||
parent_phone_2 TEXT,
|
||||
student_phone TEXT,
|
||||
grade TEXT NOT NULL,
|
||||
school_name TEXT NOT NULL,
|
||||
address TEXT NOT NULL,
|
||||
memorizing TEXT NOT NULL
|
||||
)
|
||||
''')
|
||||
print("Database initialized and 'students' table created.")
|
||||
print("Database initialized and 'students' table created with the new schema.")
|
||||
|
||||
# Command to initialize the database from the command line: flask init-db
|
||||
@app.cli.command('init-db')
|
||||
@@ -41,7 +51,8 @@ def init_db_command():
|
||||
def index():
|
||||
"""Renders the main page with the list of students."""
|
||||
conn = get_db_connection()
|
||||
students = conn.execute('SELECT * FROM students').fetchall()
|
||||
# Select all columns to display in the table
|
||||
students = conn.execute('SELECT * FROM students ORDER BY id').fetchall()
|
||||
conn.close()
|
||||
# The 'students' variable is passed to the HTML template
|
||||
return render_template('index.html', students=students)
|
||||
@@ -49,15 +60,25 @@ def index():
|
||||
@app.route('/add', methods=['POST'])
|
||||
def add_student():
|
||||
"""Handles the form submission for adding a new student."""
|
||||
# Get data from the form submitted in index.html
|
||||
name = request.form['name']
|
||||
# Get all the new data from the form submitted in index.html
|
||||
student_name = request.form['student_name']
|
||||
age = request.form['age']
|
||||
parent_name = request.form['parent_name']
|
||||
phone_number = request.form['phone_number']
|
||||
parent_phone_1 = request.form['parent_phone_1']
|
||||
parent_phone_2 = request.form.get('parent_phone_2') # .get() for optional fields
|
||||
student_phone = request.form.get('student_phone') # .get() for optional fields
|
||||
grade = request.form['grade']
|
||||
school_name = request.form['school_name']
|
||||
address = request.form['address']
|
||||
memorizing = request.form['memorizing']
|
||||
|
||||
# Insert the new student record into the database
|
||||
|
||||
# Insert the new student record into the database with all fields
|
||||
conn = get_db_connection()
|
||||
conn.execute('INSERT INTO students (name, parent_name, phone_number) VALUES (?, ?, ?)',
|
||||
(name, parent_name, phone_number))
|
||||
conn.execute('''
|
||||
INSERT INTO students (student_name, age, parent_name, parent_phone_1, parent_phone_2, student_phone, grade, school_name, address, memorizing)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
''', (student_name, age, parent_name, parent_phone_1, parent_phone_2, student_phone, grade, school_name, address, memorizing))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
@@ -65,7 +86,6 @@ def add_student():
|
||||
return redirect(url_for('index'))
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Initialize the database when the script is run directly
|
||||
init_db()
|
||||
# Run the Flask development server
|
||||
# You can remove the init_db() call from here if you are using the flask command
|
||||
# It's good practice to manage the DB initialization separately.
|
||||
app.run(debug=True)
|
||||
|
||||
المرجع في مشكلة جديدة
حظر مستخدم