漂亮的管理员界面显示用户信息,并且提供相应的查找和管理功能的html界面,并且和flask框架实现分页功能,详细代码
时间: 2024-05-06 13:15:13 浏览: 112
一款相对好用的用户管理员界面admin.html
以下是一个基于 Flask 框架和 Bootstrap 的管理员界面示例,包括用户信息的显示、查找和管理功能以及分页功能。代码如下:
```python
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import func
from math import ceil
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(50), nullable=False)
age = db.Column(db.Integer, nullable=False)
@app.route('/', methods=['GET', 'POST'])
def index():
page = request.args.get('page', 1, type=int)
name = request.args.get('name', '')
email = request.args.get('email', '')
age = request.args.get('age', '')
query = User.query.filter(User.name.like(f'%{name}%'), User.email.like(f'%{email}%'), User.age.like(f'%{age}%'))
total_count = query.count()
page_size = 5
total_page = ceil(total_count / page_size)
pagination = query.paginate(page=page, per_page=page_size)
users = pagination.items
return render_template('index.html', users=users, pagination=pagination, total_page=total_page, name=name, email=email, age=age)
@app.route('/add_user', methods=['POST'])
def add_user():
name = request.form.get('name')
email = request.form.get('email')
age = request.form.get('age')
user = User(name=name, email=email, age=age)
db.session.add(user)
db.session.commit()
return 'success'
@app.route('/delete_user', methods=['POST'])
def delete_user():
user_id = request.form.get('user_id')
user = User.query.filter_by(id=user_id).first()
db.session.delete(user)
db.session.commit()
return 'success'
if __name__ == '__main__':
app.run()
```
在 templates 目录下创建一个名为 `index.html` 的模板文件,代码如下:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Admin</title>
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.1.0/css/bootstrap.min.css">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="/">Home</a>
</li>
</ul>
</div>
</nav>
<div class="container mt-3">
<h1>Admin</h1>
<div class="row">
<div class="col-md-4">
<form>
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" name="name" value="{{ name }}">
</div>
<div class="form-group">
<label>Email</label>
<input type="text" class="form-control" name="email" value="{{ email }}">
</div>
<div class="form-group">
<label>Age</label>
<input type="text" class="form-control" name="age" value="{{ age }}">
</div>
<button type="submit" class="btn btn-primary">Search</button>
</form>
</div>
<div class="col-md-8">
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Age</th>
<th>Action</th>
</tr>
</thead>
<tbody>
{% for user in users %}
<tr>
<td>{{ user.id }}</td>
<td>{{ user.name }}</td>
<td>{{ user.email }}</td>
<td>{{ user.age }}</td>
<td>
<form class="delete-form">
<input type="hidden" name="user_id" value="{{ user.id }}">
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<nav>
<ul class="pagination">
<li class="page-item{% if pagination.page == 1 %} disabled{% endif %}">
<a class="page-link" href="{{ url_for('index', page=1, name=name, email=email, age=age) }}">First</a>
</li>
{% if pagination.has_prev %}
<li class="page-item">
<a class="page-link" href="{{ url_for('index', page=pagination.prev_num, name=name, email=email, age=age) }}">Previous</a>
</li>
{% endif %}
{% for num in pagination.iter_pages(left_edge=1, left_current=1, right_current=2, right_edge=1) %}
{% if num %}
<li class="page-item{% if pagination.page == num %} active{% endif %}">
<a class="page-link" href="{{ url_for('index', page=num, name=name, email=email, age=age) }}">{{ num }}</a>
</li>
{% else %}
<li class="page-item disabled"><a class="page-link" href="#">…</a></li>
{% endif %}
{% endfor %}
{% if pagination.has_next %}
<li class="page-item">
<a class="page-link" href="{{ url_for('index', page=pagination.next_num, name=name, email=email, age=age) }}">Next</a>
</li>
{% endif %}
<li class="page-item{% if pagination.page == pagination.pages %} disabled{% endif %}">
<a class="page-link" href="{{ url_for('index', page=pagination.pages, name=name, email=email, age=age) }}">Last</a>
</li>
</ul>
</nav>
</div>
</div>
</div>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<script>
$('.delete-form').submit(function(e) {
e.preventDefault();
var form = $(this);
$.ajax({
url: '/delete_user',
type: 'POST',
data: form.serialize(),
success: function(response) {
if (response === 'success') {
form.closest('tr').remove();
}
}
});
});
</script>
</body>
</html>
```
在 static 目录下创建一个名为 `script.js` 的 JavaScript 文件,代码如下:
```javascript
$(function() {
$('form').submit(function(e) {
e.preventDefault();
var form = $(this);
$.ajax({
url: '/add_user',
type: 'POST',
data: form.serialize(),
success: function(response) {
if (response === 'success') {
alert('User added successfully!');
form.trigger('reset');
}
}
});
});
});
```
在 static 目录下创建一个名为 `style.css` 的 CSS 文件,代码如下:
```css
body {
padding-top: 70px;
}
.btn-danger {
margin-right: 5px;
}
```
这个例子中使用 SQLite 数据库存储用户信息。你可以根据需要修改数据库连接字符串。此外,还需要在终端中安装以下依赖项:
```bash
pip install Flask Flask-SQLAlchemy
```
最后,在终端中运行以下命令启动应用程序:
```bash
export FLASK_APP=app.py
export FLASK_ENV=development
flask run
```
打开浏览器并访问 `http://localhost:5000`,即可看到管理员界面。
阅读全文