我對web開發非常陌生,我正在研究web.py
框架來開發一個小型web應用程序。假設登錄屏幕是localhost:9090/login
,在成功登錄後,它將重定向到下一頁localhost:9090/details
,並在點擊另一個按鈕add
後再次重定向到localhost:9090/details/details_entry
。在web開發中的會話和cookie的概念python
但是,當我直接嘗試localhost:9090/details
在瀏覽器上工作,並能夠看到該頁面,甚至沒有登錄。所以谷歌搜索後,很多人知道我需要使用session concept
,但我現在很忙,因爲忙於在谷歌搜索網絡概念。 任何人都可以讓我知道的
session
概念(其實爲什麼它被創建,如何登錄後,通過頁面在Python中使用它)其實什麼的
user authentication
的完整的概念, 什麼是遵循創建一個用戶登錄頁面 的步驟和要遵循的步驟登錄的用戶與細節 後會發生什麼,當用戶註銷以及如何在python會話代碼
我期待什麼是它的語言,但開發登錄屏幕和通過創建一些會話ID重定向到下一個URL的概念是相同的,所以用戶認證的概念是非常重要的,可能是這個問題對其他人有用。
被修改代碼
--------------
Login.py
import os
import sys
import web
from web import form
render = web.template.render('templates/')
urls = (
'/', 'Login',
'/projects', 'Projects',
'/project_details', 'Project_Details',
)
app = web.application(urls, globals())
web.config.debug = False
db = web.database(dbn='mysql', db='Python_Web', user='root', pw='redhat')
settings = {}
store = web.session.DBStore(db, 'sessions')
session = web.session.Session(app, store, initializer={'user': None})
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
user = db.select('user',
where = 'user_login = $username',
vars = {'username': username}
if username == user['username'] and password == user['password']:
session.user = username
raise web.seeother('/projects')
else:
return render.login_error(form)
def auth_required(func):
def proxyfunc(self, *args, **kw):
print session.user,"=======> Session stored"
try:
if session.user:
return func(self, *args, **kw)
except:
pass
raise web.seeother("/")
return proxyfunc
class Projects:
project_list = form.Form(
form.Button('Add Project'),
)
@auth_required
def GET(self):
project_form = self.project_list()
return render.projects(project_form)
def POST(self):
raise web.seeother('/project_details')
if __name__ == "__main__":
web.internalerror = web.debugerror
app.run()
在全成後上面的代碼登錄該頁面將重定向到下一頁。在這裏,我需要實現會話的概念,但我堅持要在上面的代碼中實現會話代碼的位置。任何人都可以請我指出一個正確的方法,在上面的py代碼登錄頁面上寫會話代碼。在此之後的工作需要在同一個文件的py落實執行auth_required
功能後
編輯的代碼註銷功能,得到了下面的錯誤
結果:
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/user/python_webcode/index.py", line 102, in proxyfunc
print session.user,"=======> Session Stored"
File "/usr/lib/python2.7/site-packages/web/session.py", line 71, in __getattr__
return getattr(self._data, name)
AttributeError: 'ThreadedDict' object has no attribute 'user'
看看谷歌應用程序引擎。它包括很多服務,你可以使用谷歌身份驗證。我的意思是:Google會處理幾乎所有的身份驗證。它可以隨意玩。 https://developers.google.com/appengine/docs/python/gettingstarted/usingusers – voscausa
是的,或者看看更全面的Web框架,比如Django?很會照顧會話+餅乾用戶認證爲你,只給你一個很好的抽象.. – hwjp
是的,我認爲首先使用Django,但我有小的要求只有三個像登錄頁,列表頁,formpage頁。所以我選擇了web.py框架,我ws卡在會議 –