我是web.py的初学者,并且正在使用web.py教程表单部分。我在表单库中遇到了代码以进行简单的注册。 http://webpy.org/form以及web.py0.3 http://webpy.org/docs/0.3/upgrade
的升级我正在尝试编写用于注册用户名和密码的表单,我安装了pyscopg2和postgres2.6.1以及他们在教程中使用pip推荐的其他内容。 我在practice / bin / code.py
中编写了以下代码import web
from web import form
urls = (
'/' , 'index'
)
app = web.application(urls , globals())
render = web.template.render('templates/')
signup = form.Form(
form.Textbox('Username'),
form.Password('Password'),
form.Password('Password_Again'),
validators = [form.Validator('Passwords did not match' , lambda i: i.Password == i.Password_Again)]
)
class index(object):
def GET(self):
form = signup()
return render.formtest(form = form)
def POST(self):
form = signup()
if not form.validates():
return render.formtest(form = form)
else:
db = web.database(dbn='postgres', db='test' ,host="127.0.0.1" ,user='richa9@gmail.com' ,password='123',port='5432')
login= db.insert('test' , **form.d)
return "Great success! you connected to your first database!"
if __name__ == "__main__" :
web.internalerror = web.debugerror
app.run()
内部练习/ templates / formtest.html
$def with (form)
<form name = "main" method = "POST">
$if not form.valid: <p class = "error"> Try Again! </p>
$:form.render()
<input type = "submit"/>
</form>
我研究并阅读了web.py的文档,但由于我是应用程序中的数据库新手,因此我遇到了同样的错误。当我在cmd中运行code.py并启动8080服务器时,我在cmd中获得了这个Traceback:
127.0.0.1:55677 - - [08/May/2016 13:02:53] "HTTP/1.1 POST /" - 500 Internal Server Error
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\web\application.py", line 239, in process
return self.handle()
File "C:\Python27\lib\site-packages\web\application.py", line 230, in handle
return self._delegate(fn, self.fvars, args)
File "C:\Python27\lib\site-packages\web\application.py", line 420, in _delegate
return handle_class(cls)
File "C:\Python27\lib\site-packages\web\application.py", line 396, in handle_class
return tocall(*args)
File "C:\practice\bin\code.py", line 35, in POST
login= db.insert('test' , **form.d)
File "C:\Python27\lib\site-packages\web\db.py", line 766, in insert
db_cursor = self._db_cursor()
File "C:\Python27\lib\site-packages\web\db.py", line 566, in _db_cursor
return self.ctx.db.cursor()
File "C:\Python27\lib\site-packages\web\db.py", line 507, in _getctx
self._load_context(self._ctx)
File "C:\Python27\lib\site-packages\web\db.py", line 518, in _load_context
ctx.db = self._connect(self.keywords)
File "C:\Python27\lib\site-packages\web\db.py", line 960, in _connect
conn = DB._connect(self, keywords)
File "C:\Python27\lib\site-packages\web\db.py", line 546, in _connect
return self.db_module.connect(**keywords)
File "C:\Python27\lib\site-packages\psycopg2\__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
OperationalError: could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
当我输入不匹配的密码时,formtest.html会响应,但不会响应数据库系统。有什么建议可以解决吗?提前谢谢。
答案 0 :(得分:0)
这里的问题是您需要在主机上设置postgresql服务器才能完成练习。
在Windows上,您需要下载windows installer,然后follow the documentation下载如何安装服务器。
安装服务器后,您需要创建一个登录角色和数据库,并将该角色分配给数据库,然后才能继续。
为此,请使用作为服务器一部分安装的pgadminIII
工具;并按照以下步骤操作:
Servers
下的Server Groups
树。Login Roles
,然后选择New Role..
Databases
,然后选择New Database..
然后,在您的代码中,您需要调整此行:
db = web.database(dbn='postgres',
db='test', # replace
user='richa9@gmail.com', # replace
password='123', # replace
port='5432', host="127.0.0.1")
并更改相应的值。
重要 - 即使您按照这些说明操作,您的代码也可能会失败,因为您还没有在数据库中创建任何表并且您正在尝试插入记录。查看web.py文档,了解本教程需要哪些表。