如何使用webpy修复python中的500个内部服务器错误?

2024-04-25 12:48:39 发布

您现在位置:Python中文网/ 问答频道 /正文

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

在脚本.js公司名称:

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

        }
    });

以及主.py公司名称:

^{pr2}$

我的结果是:

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

Tags: inpyselfwebhomereturnlibpackages
2条回答

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

查询序列化使用输入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.

相关问题 更多 >