python - Flask:参数错误 - devices() 没有参数(给定 3 个)
问题描述
我是新来的烧瓶。我想将数据从表单添加到数据库,但出现此错误。
devices() 不带参数(给定 3 个)
我为另一个函数编写了这段代码,并正确地将数据添加到数据库中。但是对于这个功能(设备)不起作用并且有错误。
这是我的home.py:
class devices(db.Model):
id = db.Column('device_id', db.Integer, primary_key = True)
device_name = db.Column(db.String(80))
device_description = db.Column(db.String(200))
device_model = db.Column(db.String(80))
def __init__(self, device_name, device_description, device_model):
self.device_name = device_name
self.device_description = device_description
self.device_model = device_model
@app.route("/devices", methods=['POST'])
def devices():
return render_template("devices.html")
@app.route("/show_all_device")
def show_all_device():
return render_template('show_all_device.html', devices = devices.query.all() )
@app.route("/device_add", methods=['GET', 'POST'])
def device_add():
if request.method == 'POST':
device = devices(request.form['device_name'], request.form['device_description'],
request.form['device_model'])
db.session.add(device)
db.session.commit()
flash('Record was successfully added')
return redirect(url_for('show_all_device'))
return render_template('devices.html')
这是 device.html
{% block content %}
<h2 style="margin: auto; text-align: center;">Devices</h2>
<form action="/device_add" method="POST">
<br>
<p style="display: inline-block;">Device Name:</p> <input type="text" name="device_name">
<br>
<p style="display: inline-block;">Description:</p> <textarea style="margin-top: 20px;" name="device_description"></textarea>
<br>
<p style="display: inline-block;">Model:</p> <input type="text" name="device_model">
<br><br>
<input type="submit" name="structure" value="Add Devices">
</form>
{% endblock %}
和 show_all_device.html:
<!DOCTYPE html>
<html lang = "en">
<head></head>
<body>
<table border="1" style="height: 300px; width: 1000px; text-align: center;">
<thead>
<tr>
<th>Device Name</th>
<th>Description</th>
<th>Model</th>
</tr>
</thead>
<tbody>
{% for device in devices %}
<tr>
<td>{{ device.device_name }}</td>
<td>{{ device.device_description }}</td>
<td>{{ device.device_model }}</td>
<td><a href="{{ url_for('device_edit', id=device.id) }}">Edit</a> ~ <a href="{{ url_for('device_delete', id=device.id) }}">Delete</a></td>
</tr>
{% endfor %}
</tbody>
</table>
<a href="{{ url_for('device_add') }}">Add New Region</a>
谁能帮我?
解决方案
class devices(db.Model):
...
def devices():
....
您有一个名为 的类和一个函数devices
。不要那样做。
推荐阅读
- flutter - 异步功能完成后颤振 setState()
- postgresql - 改进简单连接
- node.js - Multer 不使用 next-connect 返回 req.body 和 req.file
- javascript - typeerror: app.use() 需要中间件功能修复
- java - 由于库存不足,iPhoneX 的购物车数量已从 10 个减少到 9 个
- spring - 如何通过使用 Postgres 代码的 Flyway 迁移创建 DataJpaTest?
- javascript - HEROKU 错误:ENOENT:没有这样的文件或目录,stat '/app/distpublic/index.html'
- android - 保持DatabaseHelper打开或关闭并在android应用程序上重新打开更好吗?
- firebase - Firebase 用户身份验证管理问题
- javascript - 如何在通过 React useHistory 传递道具时有条件地渲染道具并检查未定义的道具?