首页 > 解决方案 > 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>

谁能帮我?

标签: pythondatabasesqliteflasktypeerror

解决方案


class devices(db.Model):
    ...
def devices():
    ....

您有一个名为 的类和一个函数devices。不要那样做。


推荐阅读