1

我正在使用 python web.py 框架来设计一个小型 Web 应用程序,下面是我的 index.py 代码

索引.py

import web
from web import form
import MySQLdb as mdb



render = web.template.render('templates/')

urls = (
  '/',   'Login',
  '/projects',  'Projects',
)

app = web.application(urls, globals())
conn = mdb.connect(user='root', passwd='redhat', db='Python_Web', host='localhost')

class Login:

    login_form = form.Form( 
        form.Textbox('username', form.notnull),
        form.Password('password', form.notnull),
        form.Button('Login'),
    )

    def GET(self):
        form = self.login_form()
        return render.login(form)

    def POST(self):
        if not self.login_form.validates():
            return render.login(self.login_form)
        i = web.input()
        username = i.username
        password = i.password
        query = "select user_login,user_password from user where user_login = '%s' " % username
        cur = conn.cursor()
        cur.execute( query )
        user_details = cur.fetchone()
        if username == user_details[0] and password == user_details[1]:
            raise web.seeother('/projects')
        else:
            return render.login_error(form)    

if __name__ == "__main__":
    web.internalerror = web.debugerror
    app.run()     

当我在浏览器中运行上述文件时localhost:8080 ,我可以看到其中包含代码login.html并以以下方式访问的登录页面render.login(form)

但是当用户名和密码匹配时,我的意图是重定向到另一个页面(projects.py)并如上web.seeother('/projects')所示。现在这将进入项目类并显示该类的 html 页面

但是当我单击登录按钮时,我看到以下错误

Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/web/application.py", line 239, in process
    return self.handle()
  File "/usr/lib/python2.7/site-packages/web/application.py", line 230, in handle
    return self._delegate(fn, self.fvars, args)
  File "/usr/lib/python2.7/site-packages/web/application.py", line 420, in _delegate
    return handle_class(cls)
  File "/usr/lib/python2.7/site-packages/web/application.py", line 396, in handle_class
    return tocall(*args)
  File "/home/local/usser/python_webcode/index.py", line 55, in GET
    app.run()
  File "/usr/lib/python2.7/site-packages/web/template.py", line 881, in __call__
    return BaseTemplate.__call__(self, *a, **kw)
  File "/usr/lib/python2.7/site-packages/web/template.py", line 808, in __call__
    return self.t(*a, **kw)
TypeError: __template__() takes exactly 1 argument (0 given)

谁能帮我解决上述错误

同样在 web.py 如何重定向到另一个具有 html 文件链接的类

4

1 回答 1

0

控制器中可能存在错误Projects,您没有将所需的参数传递给模板渲染。

于 2012-10-10T13:51:50.553 回答