Skip to content
Snippets Groups Projects
Commit 3c5f7967 authored by Ziyan Lu's avatar Ziyan Lu
Browse files

main

parent 73e78381
No related branches found
No related tags found
No related merge requests found
app.py 0 → 100644
from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
import os
app = Flask(__name__)
DB_URI = os.environ.get('DATABASE_URI')
if DB_URI is None:
raise ValueError('No database URI set in environment variable DATABASE_URI')
app.config['SQLALCHEMY_DATABASE_URI'] = DB_URI
db = SQLAlchemy(app)
class Questions(db.Model):
id = db.Column(db.Integer, primary_key=True)
question = db.Column(db.String(255), nullable=False)
option1 = db.Column(db.String(255), nullable=False)
option2 = db.Column(db.String(255), nullable=False)
option3 = db.Column(db.String(255), nullable=False)
option4 = db.Column(db.String(255), nullable=False)
answer = db.Column(db.String(255), nullable=False)
feedback = db.Column(db.String(255))
def delete(self):
db.session.delete(self)
db.session.commit()
@app.route('/')
def index():
return render_template("index.html")
@app.route('/questions')
def questions():
questions = Questions.query.all()
return render_template("questions.html", questions=questions)
@app.route('/add_question', methods=['GET', 'POST'])
def add_question():
if request.method == 'POST':
question = request.form['question']
option1 = request.form['option1']
option2 = request.form['option2']
option3 = request.form['option3']
option4 = request.form['option4']
answer = request.form['answer']
feedback = request.form['feedback']
new_question = Questions(question=question, option1=option1, option2=option2, option3=option3, option4=option4, answer=answer, feedback=feedback)
db.session.add(new_question)
db.session.commit()
return redirect(url_for('questions'))
return render_template("add_question.html")
@app.route('/delete_question/<int:id>', methods=['POST'])
def delete_question(id):
question = Questions.query.get(id)
question.delete()
return redirect(url_for('questions'))
if __name__ == '__main__':
app.run(debug=True)
{% extends "layout.html" %}
{% block content %}
<div class="container">
<h1 class="mt-3">Add Question</h1>
<form method="POST" action="{{ url_for('add_question') }}" class="mt-3">
<div class="form-group">
<label for="question">Question:</label>
<input type="text" name="question" class="form-control" required>
</div>
<div class="form-group">
<label for="option1">Option 1:</label>
<input type="text" name="option1" class="form-control" required>
</div>
<div class="form-group">
<label for="option2">Option 2:</label>
<input type="text" name="option2" class="form-control" required>
</div>
<div class="form-group">
<label for="option3">Option 3:</label>
<input type="text" name="option3" class="form-control" required>
</div>
<div class="form-group">
<label for="option4">Option 4:</label>
<input type="text" name="option4" class="form-control" required>
</div>
<div class="form-group">
<label for="answer">Answer:</label>
<input type="text" name="answer" class="form-control" required>
</div>
<div class="form-group">
<label for="feedback">Feedback:</label>
<input type="text" name="feedback" class="form-control">
</div>
<button type="submit" class="btn btn-primary">Add Question</button>
</form>
</div>
{% endblock content %}
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<title>Edit Question</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
<h1>Edit Question</h1>
<form method="POST" action="{{ url_for('edit_question', question_id=question.id) }}">
<label for="question">Question:</label>
<input type="text" name="question" value="{{ question.question }}" required>
<br>
<label for="option1">Option 1:</label>
<input type="text" name="option1" value="{{ question.option1 }}" required>
<br>
<label for="option2">Option 2:</label>
<input type="text" name="option2" value="{{ question.option2 }}" required>
<br>
<label for="option3">Option 3:</label>
<input type="text" name="option3" value="{{ question.option3 }}" required>
<br>
<label for="option4">Option 4:</label>
<input type="text" name="option4" value="{{ question.option4 }}" required>
<br>
<label for="answer">Answer:</label>
<input type="text" name="answer" value="{{ question.answer }}" required>
<br>
<label for="feedback">Feedback:</label>
<input type="text" name="feedback" value="{{ question.feedback }}">
<br>
<button type="submit">Update Question</button>
</form>
</body>
</html>
{% extends "layout.html" %}
{% block content %}
{% endblock content %}
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"
crossorigin="anonymous"
/>
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"
crossorigin="anonymous"
/>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="collapse navbar-collapse" id="navbar">
<div class="navbar-nav">
<a href="{{ url_for('index') }}" class="nav-item nav-link">Home</a>
<a href="{{ url_for('questions') }}" class="nav-item nav-link">Delete question</a>
<a href="{{ url_for('add_question') }}" class="nav-item nav-link">Add question</a>
</div>
</div>
</nav>
<div id="content">
{% block content %}
{% endblock %}
</div>
</body>
</html>
\ No newline at end of file
{% extends "layout.html" %}
{% block content %}
<div class="container">
<table class="table table-bordered">
<thead>
<tr>
<th>Question</th>
<th>Option 1</th>
<th>Option 2</th>
<th>Option 3</th>
<th>Option 4</th>
<th>Answer</th>
<th>Action</th>
</tr>
</thead>
<tbody>
{% for question in questions %}
<tr>
<td>{{ question.question }}</td>
<td>{{ question.option1 }}</td>
<td>{{ question.option2 }}</td>
<td>{{ question.option3 }}</td>
<td>{{ question.option4 }}</td>
<td>{{ question.answer }}</td>
<td>
<form method="POST" action="{{ url_for('delete_question', id=question.id) }}">
<button class="btn btn-danger" type="submit">Delete</button>
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock content %}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment