By building web Interface , stay web Add, delete, modify and query the database on the interface . use python Medium web Module complete .
At present, there are not many similar contents on the Internet , So there are a lot of places to try out by yourself .
Besides, here python The program requires users to contact their HTML file , In this experiment , I did it by putting part python Put the code HTML in , Reduce the workload of code , Later, it was considered that URL Reasons for , So put all the programs in one file .
import web import model # webpy Module has URL Matching function urls = ( '/','index', '/index1',
'index1', '/index2', 'index2', '/index3', 'index3', '/index4', 'index4',
'/index5', 'index5', '/add','add', '/delete','delete', '/change','change',
'/select','select', '/turn_add','turn_add', '/turn_change','turn_change',
'/turn_delete','turn_delete', '/turn_select','turn_select', '/returnback',
'returnback' ) # Determine program directory , Linked database app = web.application(urls, globals()) render =
web.template.render('templates/') db = web.database(dbn='', host='', port=, db=
'', user='', pw='') # Presentation layer ,web Home page of . class index: def GET(self): return render.
index() # Business logic layer , Problem specific operations class turn_add: def POST(self): print("touch it\n")
raise web.seeother('/index1') class turn_delete: def POST(self): print("touch
it\n") raise web.seeother('/index2') class turn_change: def POST(self): print(
"touch it\n") raise web.seeother('/index3') class turn_select: def POST(self):
print("touch it\n") raise web.seeother('/index4') class returnback: def POST(
self): raise web.seeother('/') # Or presentation layer class index: def GET(self): return render
.index() class index1: def GET(self): return render.index1() class index2: def
GET(self): return render.index2() class index3: def GET(self): return render.
index3() class index4: def GET(self): return render.index4() class index5: def
GET(self): return render.index5(email) # Data access layer , Statements that directly manipulate the database class add: def POST(
self): i = web.input() print(i. user ID) if i. user ID!=' user ID' and i. user name !=' user name ' and i.
Personal profile !=' Personal profile ' and i. Industry !=' Industry ': n = db.insert('user', user ID=i. user ID, user name =i. user name ,
Personal profile =i. Personal profile , Industry =i. Industry ) raise web.seeother('/')
#POST Method received a post And after processing , It will send a message to the browser 303 News and new website else: raise web.seeother('/') class
delete: def POST(self): i = web.input() print(i) a=i. user ID print(a) db.delete(
'user',where=' user ID = $a',vars=locals()) raise web.seeother('/') class change:
def POST(self): i=web.input() print(i) a=i. user ID b=i. user name if b!=' user name ': db.update(
'user',where=' user ID=$a',vars=locals(), user name =i. user name ) c=i. Personal profile if c!=' Personal profile ': db.
update('user',where=' user ID=$a',vars=locals(), Personal profile =i. Personal profile ) d=i. Industry if d!=' Industry ':
db.update('user',where=' user ID=$a',vars=locals(), Industry =i. Industry ) #
db.update('user',where=' user ID=$a',vars=locals(),
user name =i. user name , Personal profile =i. Personal profile , Industry =i. Industry ) raise web.seeother('/') class select: def
POST(self): i=web.input() print(i) a=i. user ID if i. user ID==' user ID': information = db.
select('user') print(information) return render.index5(information) else: #
The reason to add vars=locals() The reason for this code , It's really, really important !!!!!( For a long time ) # Update and return the current local symbol table as a dictionary .
Free variables are defined by the locals() return , Not by class Block to return . It should be noted that , locals() The dictionary should not be modified ; Modifying behavior in the interpreter may
local Invalid value or free variable . # return __dict__ attribute , Such as module , class , example , Or other have __dict__ Attribute object. such as
Modules and instances have updatable __dict__ attribute ; # However, other objects May be right __dict__ Write restrictions for property ( for example Class use dictproxy
Prevent direct dictionary updates ).vars() If parameters are not transmitted # So the role and locals() equally . It should be noted that , locals The dictionary is only used for reading operations , Because yes
locals Updates to will be ignored . information = db.select('user',where=' user ID=$a',vars=locals())
return render.index5(information) if __name__ == "__main__": app.run()
Technology
Daily Recommendation