【问题标题】:How to fix 500 Internal Server Error / web.py如何修复 500 内部服务器错误 / web.py
【发布时间】:2019-08-15 19:47:43
【问题描述】:

我收到此错误,我正在尝试使用 web.py 创建一个简单的 Web 项目,不幸的是,当我尝试注册用户时,我收到了错误,我已经尝试更改属性但无法解决问题

File "C:\Users\Pippo\Anaconda3\lib\site-packages\web\utils.py", line 75, in   __getattr__
    return self[key]
KeyError: 'name'

During handling of the above exception, another exception occurred:


Traceback (most recent call last):
  File "C:\Users\Pippo\Anaconda3\lib\site-packages\web\application.py", line 257, in process
    return self.handle()
  File "C:\Users\Pippo\Anaconda3\lib\site-packages\web\application.py", line 248, in handle
    return self._delegate(fn, self.fvars, args)
  File "C:\Users\Pippo\Anaconda3\lib\site-packages\web\application.py", line 488, in _delegate
    return handle_class(cls)
  File "C:\Users\Pippo\Anaconda3\lib\site-packages\web\application.py", line 466, in handle_class
    return tocall(*args)
  File "controller.py", line 57, in POST
    reg_model.insert_user(data)
  File "C:\Users\Pippo\Desktop\UDEMY\The Complete Python 3 Course - Beginner to Advanced\Web development project Using web.py\CodeWizzard\Models\RegisterModel.py", line 15, in insert_user
    id = self.Users.insert({"username": data.username, "name": data.name, "password": hashed,
  File "C:\Users\Pippo\Anaconda3\lib\site-packages\web\utils.py", line 77, in __getattr__
    raise AttributeError(k)
AttributeError: 'name'


127.0.0.1:55572 - - [15/Aug/2019 20:54:31] "HTTP/1.1 POST /postregistration" - 500 Internal Server Error

我已经检查了文件(Main.py、RegistrationModel.py 和 Register.HTML),但没有找到错误。

urls = (
    '/', 'Home',
    '/register', 'Register',
    '/login', 'Login',
    '/logout', 'Logout',
    '/postregistration', 'PostRegistration',
)


app = web.application(urls, globals())
session = web.session.Session(app, web.session.DiskStore("session"), initializer={"user": None})
session_data = session._initializer

render = web.template.render("Views/Templates", base = "Main",
                         globals={'session': session_data, 'current_user': session_data["user"]})


class PostRegistration:

  def POST(self):
    data = web.input()

    reg_model = RegisterModel.RegisterModel()
    reg_model.insert_user(data)

    return data.username

'''
RegistrationModel.py
'''

class RegisterModel:

  def __init__(self):
      self.client = MongoClient()
      self.db = self.client.codewizard
      self.Users = self.db.users

  def insert_user(self, data):
      hashed = bcrypt.hashpw(data.password.encode(), bcrypt.gensalt())

      id = self.Users.insert({"username": data.username, "name":   data.name, "password": hashed,
                            "email": data.email, "avatar": "", "background": "", "about": "",
                            "hobbies": "", "birthday": ""})
   print("uid is", id)

'''
Register.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 name = "username" 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 name = "display_name" 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 name = "email" 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 name = "password" id = "password" class = "form-control" type = "password" placeholder = "Make a password" />
    </div>

    <button type = "submit" class = "btn btn-info waves-effect">Submit</button>
</form>

任何想法为什么我有这个错误?

感谢您的帮助!

【问题讨论】:

  • 您的 data 对象似乎没有 name 变量。另外,您能否编辑您的帖子,使错误出现在三次勾号中,使其看起来像代码,并添加一些细节?我试图编辑它,但不知道要添加哪些细节。
  • RegistrationModel.py 你使用data.name 这会产生问题。似乎data 没有字段name。在你的表格中,我看不到&lt;input&gt;name="name",而是name="display_name"。试试data.display_name 而不是data.name
  • @Manson Caiby 首先感谢编辑!
  • @furas 非常感谢您的帮助,问题实际上出在 register.html 我只是将name = "display_name" 更改为name = "name",现在它可以工作了!非常感谢!

标签: python pymongo web.py


【解决方案1】:

您似乎没有在 POST 请求的有效负载中传递 name

您可以使用forms 模块来验证输入。

http://webpy.org/cookbook/forms

如果name 是可选输入,则为web.input 指定默认值。

data = web.input(name="default-name")

【讨论】:

    猜你喜欢
    • 2019-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    相关资源
    最近更新 更多