漂亮的管理员界面显示用户信息,并且提供相应的查找和管理功能的html界面,并且和flask框架实现分页功能。前后端代码
时间: 2024-05-08 22:22:07 浏览: 109
一款漂亮的前端界面
前端代码(使用Bootstrap框架):
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>User Management</title>
<!-- Bootstrap core CSS -->
<link href="https://cdn.bootcss.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div class="container-fluid">
<h1 class="text-center my-5">User Management</h1>
<div class="row">
<div class="col-md-6">
<form class="form-inline mb-4">
<input type="text" class="form-control mr-2" placeholder="Search">
<button type="submit" class="btn btn-primary">Search</button>
</form>
</div>
<div class="col-md-6 text-right">
<button type="button" class="btn btn-success" data-toggle="modal" data-target="#add-user-modal">Add User</button>
</div>
</div>
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for user in users %}
<tr>
<td>{{ user.id }}</td>
<td>{{ user.name }}</td>
<td>{{ user.email }}</td>
<td>{{ user.phone }}</td>
<td>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#edit-user-modal" data-id="{{ user.id }}" data-name="{{ user.name }}" data-email="{{ user.email }}" data-phone="{{ user.phone }}">Edit</button>
<button type="button" class="btn btn-danger" data-toggle="modal" data-target="#delete-user-modal" data-id="{{ user.id }}">Delete</button>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<nav aria-label="Page navigation">
<ul class="pagination justify-content-center">
{% if page > 1 %}
<li class="page-item"><a class="page-link" href="?page={{ page-1 }}">Previous</a></li>
{% endif %}
{% for i in range(max_page) %}
{% if i+1 == page %}
<li class="page-item active"><a class="page-link" href="?page={{ i+1 }}">{{ i+1 }}</a></li>
{% else %}
<li class="page-item"><a class="page-link" href="?page={{ i+1 }}">{{ i+1 }}</a></li>
{% endif %}
{% endfor %}
{% if page < max_page %}
<li class="page-item"><a class="page-link" href="?page={{ page+1 }}">Next</a></li>
{% endif %}
</ul>
</nav>
</div>
<!-- Add user modal -->
<div class="modal fade" id="add-user-modal" tabindex="-1" role="dialog" aria-labelledby="add-user-modal-label" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form action="{{ url_for('add_user') }}" method="post">
<div class="modal-header">
<h5 class="modal-title" id="add-user-modal-label">Add User</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" name="name" required>
</div>
<div class="form-group">
<label>Email</label>
<input type="email" class="form-control" name="email" required>
</div>
<div class="form-group">
<label>Phone</label>
<input type="tel" class="form-control" name="phone" required>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Add</button>
</div>
</form>
</div>
</div>
</div>
<!-- Edit user modal -->
<div class="modal fade" id="edit-user-modal" tabindex="-1" role="dialog" aria-labelledby="edit-user-modal-label" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form action="{{ url_for('edit_user') }}" method="post">
<div class="modal-header">
<h5 class="modal-title" id="edit-user-modal-label">Edit User</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" name="name" required>
</div>
<div class="form-group">
<label>Email</label>
<input type="email" class="form-control" name="email" required>
</div>
<div class="form-group">
<label>Phone</label>
<input type="tel" class="form-control" name="phone" required>
</div>
<input type="hidden" name="id">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
</div>
</div>
</div>
<!-- Delete user modal -->
<div class="modal fade" id="delete-user-modal" tabindex="-1" role="dialog" aria-labelledby="delete-user-modal-label" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form action="{{ url_for('delete_user') }}" method="post">
<div class="modal-header">
<h5 class="modal-title" id="delete-user-modal-label">Delete User</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Are you sure you want to delete this user?
<input type="hidden" name="id">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-danger">Delete</button>
</div>
</form>
</div>
</div>
</div>
<!-- Bootstrap core JavaScript -->
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<script>
$('#edit-user-modal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget)
var id = button.data('id')
var name = button.data('name')
var email = button.data('email')
var phone = button.data('phone')
var modal = $(this)
modal.find('input[name="id"]').val(id)
modal.find('input[name="name"]').val(name)
modal.find('input[name="email"]').val(email)
modal.find('input[name="phone"]').val(phone)
})
$('#delete-user-modal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget)
var id = button.data('id')
var modal = $(this)
modal.find('input[name="id"]').val(id)
})
</script>
</body>
</html>
```
后端代码(使用Flask框架):
```python
from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
email = db.Column(db.String(120), nullable=False, unique=True)
phone = db.Column(db.String(20), nullable=False)
@app.route('/')
def index():
page = int(request.args.get('page', 1))
per_page = 10
users = User.query.order_by(User.id).paginate(page, per_page=per_page)
max_page = users.pages
return render_template('index.html', users=users.items, page=page, max_page=max_page)
@app.route('/add_user', methods=['POST'])
def add_user():
name = request.form['name']
email = request.form['email']
phone = request.form['phone']
user = User(name=name, email=email, phone=phone)
db.session.add(user)
db.session.commit()
return redirect(url_for('index'))
@app.route('/edit_user', methods=['POST'])
def edit_user():
id = request.form['id']
name = request.form['name']
email = request.form['email']
phone = request.form['phone']
user = User.query.get(id)
user.name = name
user.email = email
user.phone = phone
db.session.commit()
return redirect(url_for('index'))
@app.route('/delete_user', methods=['POST'])
def delete_user():
id = request.form['id']
user = User.query.get(id)
db.session.delete(user)
db.session.commit()
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(debug=True)
```
阅读全文