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

How fix 500 Internal Server Error in python using webpy?

当我在我的 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

query serialize 使用输入 name 属性,而不是 id 属性。您的 html 代码段似乎不包含 name="username".

之类的内容

来自 jquery 文档:

Note: Only "successful controls" are serialized to the string. No submit button value is serialized since the form was not submitted using a button. For a form element's value to be included in the serialized string, the element must have a name attribute. Values from checkboxes and radio buttons (inputs of type "radio" or "checkbox") are included only if they are checked. Data from file select elements is not serialized.

这是我的注册表单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>