如何使用webpy修复python 500 Internal Server Error?

时间:2019-04-01 14:40:15

标签: python python-3.x web.py

当我在scripty.js文件和main.py文件中添加了几行代码时,我的POST函数遇到了一些问题:

scripty.js:

 var form = $('#register-form').serialize();
    $.ajax({
        url: '/postreg',
        type: 'POST',
        data: form,
        success: function (res) {
            res.preventDefault()
            console.log("done");

        }
    });

和main.py:

class PostRegistration:
    def POST(self):
        data = web.input()
        return data.username

有我的结果:

127.0.0.1:55126 - - [01/Apr/2019 18:37:25] "HTTP/1.1 GET /static/js/ripples.min.js.map" - 200 

Traceback (most recent call last):
  File "/home/amir/.local/lib/python3.6/site-packages/web/utils.py", line 70, in __getattr__
    return self[key]
KeyError: 'username'


During handling of the above exception, another exception occurred:


Traceback (most recent call last):
  File "/home/amir/.local/lib/python3.6/site-packages/web/application.py", line 257, in process
    return self.handle()

  File "/home/amir/.local/lib/python3.6/site-packages/web/application.py", line 248, in handle

    return self._delegate(fn, self.fvars, args)
  File "/home/amir/.local/lib/python3.6/site-packages/web/application.py", line 488, in _delegate
    return handle_class(cls)

  File "/home/amir/.local/lib/python3.6/site-packages/web/application.py", line 466, in handle_class
    return tocall(*args)

  File "/home/amir/PycharmProjects/SocialWeb/Controller.py", line 27, in POST
    return data.username
  File "/home/amir/.local/lib/python3.6/site-packages/web/utils.py", line 72, in __getattr__
    raise AttributeError(k)
AttributeError: 'username'

127.0.0.1:55124 - - [01/Apr/2019 18:37:30] "HTTP/1.1 POST /postreg" - 500 Internal Server Error

2 个答案:

答案 0 :(得分:0)

查询序列化使用输入name属性,而不使用id属性。您的html片段似乎不包含name="username"之类的内容。

来自jquery文档:

  

注意:只有“成功控件”才被序列化为字符串。由于没有使用按钮提交表单,因此没有序列化提交按钮的值。若要将表单元素的值包含在序列化的字符串中,该元素必须具有 name 属性。仅当选中复选框和单选按钮(“ radio”或“ checkbox”类型的输入)中的值时,才包括这些值。来自文件选择元素的数据未序列化。

答案 1 :(得分:0)

这是我的注册表HTML文件

<div class="container">
    <h2>Register Account</h2>

    <br /><br />

    <form id="register-form">

        <div class="form-group  label-static is-empty">
            <label for="username" class="control-label">Username</label>
            <input  id="username" class="form-control" type="text" placeholder="Choose a Username">
        </div>

        <div class="form-group  label-static is-empty">
            <label for="display_name" class="control-label">Full Name</label>
            <input id="display_name" class="form-control" type="text" placeholder="Enter your full name">

        </div>
        <div class="form-group  label-static is-empty">
            <label for="email" class="control-label">Email Address</label>
            <input id="email" class="form-control" type="email" placeholder="Enter your email">

        </div>
        <div class="form-group  label-static is-empty">
            <label for="password" class="control-label">Password</label>
            <input id="password" class="form-control" type="password" placeholder="Choose a password">

        </div>

        <button type="submit" class="btn btn-raised btn-info ">Submit <div class="ripple-container"></div> </button>
    </form>
</div>