用python和vue生成一个用户管理系统
时间: 2024-05-10 12:15:50 浏览: 157
后端用java加前端vue搭建的用户管理系统
5星 · 资源好评率100%
好的,这里提供一个简单的用户管理系统示例,使用Python Flask作为后端和Vue.js作为前端。
后端部分(Python Flask):
1. 创建一个虚拟环境并安装Flask:
```
python3 -m venv env
source env/bin/activate
pip install Flask
```
2. 创建一个名为app.py的Flask应用程序:
```python
from flask import Flask, jsonify, request
app = Flask(__name__)
users = [
{"id": 1, "name": "John Doe", "email": "john@example.com"},
{"id": 2, "name": "Jane Doe", "email": "jane@example.com"}
]
@app.route('/users', methods=['GET'])
def get_users():
return jsonify(users)
@app.route('/users', methods=['POST'])
def create_user():
user = {
"id": len(users) + 1,
"name": request.json['name'],
"email": request.json['email']
}
users.append(user)
return jsonify(user)
@app.route('/users/<int:user_id>', methods=['GET'])
def get_user(user_id):
user = next((user for user in users if user["id"] == user_id), None)
return jsonify(user)
@app.route('/users/<int:user_id>', methods=['PUT'])
def update_user(user_id):
user = next((user for user in users if user["id"] == user_id), None)
if user:
user.update(request.json)
return jsonify(user)
else:
return jsonify({"error": "User not found"}), 404
@app.route('/users/<int:user_id>', methods=['DELETE'])
def delete_user(user_id):
user = next((user for user in users if user["id"] == user_id), None)
if user:
users.remove(user)
return jsonify({"message": "User deleted successfully"})
else:
return jsonify({"error": "User not found"}), 404
if __name__ == '__main__':
app.run(debug=True)
```
3. 运行应用程序:
```
python app.py
```
前端部分(Vue.js):
1. 安装Vue CLI:
```
npm install -g @vue/cli
```
2. 创建一个名为user-management的Vue项目:
```
vue create user-management
```
3. 安装axios和bootstrap-vue:
```
cd user-management
npm install axios bootstrap-vue
```
4. 编辑src/App.vue文件,添加以下代码:
```html
<template>
<div class="container">
<h1>User Management</h1>
<form @submit.prevent="createUser">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" class="form-control" id="name" v-model="newUser.name">
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email" v-model="newUser.email">
</div>
<button type="submit" class="btn btn-primary">Create User</button>
</form>
<hr>
<div class="row">
<div class="col-sm-4" v-for="user in users" :key="user.id">
<div class="card">
<div class="card-body">
<h5 class="card-title">{{ user.name }}</h5>
<p class="card-text">{{ user.email }}</p>
<button class="btn btn-primary" @click="editUser(user)">Edit</button>
<button class="btn btn-danger" @click="deleteUser(user)">Delete</button>
</div>
</div>
</div>
</div>
<b-modal v-model="showModal" title="Edit User">
<form @submit.prevent="updateUser">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" class="form-control" id="name" v-model="currentUser.name">
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email" v-model="currentUser.email">
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>
</b-modal>
</div>
</template>
<script>
import axios from 'axios'
import { BModal } from 'bootstrap-vue'
export default {
name: 'App',
components: {
BModal
},
data () {
return {
users: [],
newUser: {
name: '',
email: ''
},
currentUser: {
id: null,
name: '',
email: ''
},
showModal: false
}
},
created () {
this.getUsers()
},
methods: {
getUsers () {
axios.get('http://localhost:5000/users')
.then(response => {
this.users = response.data
})
.catch(error => {
console.log(error)
})
},
createUser () {
axios.post('http://localhost:5000/users', this.newUser)
.then(response => {
this.users.push(response.data)
this.newUser = {
name: '',
email: ''
}
})
.catch(error => {
console.log(error)
})
},
editUser (user) {
this.currentUser = { ...user }
this.showModal = true
},
updateUser () {
axios.put(`http://localhost:5000/users/${this.currentUser.id}`, this.currentUser)
.then(response => {
const index = this.users.findIndex(user => user.id === this.currentUser.id)
this.users[index] = response.data
this.currentUser = {
id: null,
name: '',
email: ''
}
this.showModal = false
})
.catch(error => {
console.log(error)
})
},
deleteUser (user) {
axios.delete(`http://localhost:5000/users/${user.id}`)
.then(response => {
const index = this.users.findIndex(u => u.id === user.id)
this.users.splice(index, 1)
})
.catch(error => {
console.log(error)
})
}
}
}
</script>
<style>
.container {
margin-top: 50px;
}
</style>
```
5. 运行应用程序:
```
npm run serve
```
现在您应该可以在http://localhost:8080中看到用户管理系统了!
阅读全文