flask:参数错误-devices()不带参数(给定3个)

时间:2019-02-25 17:21:10

标签: python database sqlite flask typeerror

我是烧瓶新手。我想将数据从表单添加到数据库,但是出现此错误。

  

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>

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

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

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