小編給大家分享一下Python中Django如何實現(xiàn)簡單注冊功能,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
python的數據類型:1. 數字類型,包括int(整型)、long(長整型)和float(浮點型)。2.字符串,分別是str類型和unicode類型。3.布爾型,Python布爾類型也是用于邏輯運算,有兩個值:True(真)和False(假)。4.列表,列表是Python中使用最頻繁的數據類型,集合中可以放任何數據類型。5. 元組,元組用”()”標識,內部元素用逗號隔開。6. 字典,字典是一種鍵值對的集合。7. 集合,集合是一個無序的、不重復的數據組合。
項目創(chuàng)建略,可參考Python Django Vue 項目創(chuàng)建。
目錄結構如下
編輯views.py
from django.shortcuts import render # Create your views here. from django.http import HttpResponse from django.shortcuts import render from common.DBHandle import DataBaseHandle import time def djangoHello(request): return HttpResponse('Hello Django!') def index(request): return render(request,'index.html') def login(request): print('login_func') usn = request.POST['username'] pwd = request.POST['password'] host = '127.0.0.1' username = 'username' password = 'password' database = 'dbname' port = 3306 # 實例化 數據庫 連接 DbHandle = DataBaseHandle(host, username, password, database, port) localTime = time.localtime(time.time()) create_time = time.strftime("%Y-%m-%d %H:%M:%S", localTime) sql = "insert into user(username,password,create_time) values ('%s','%s','%s')" % (usn, pwd, create_time) DbHandle.insertDB(sql) DbHandle.closeDb() return render(request,'login.html')
接下來編輯urls.py
"""FirstWeb URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/2.1/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: path('', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin from django.urls import path from fistWeb import views urlpatterns = [ path('admin/', admin.site.urls), path('hello/',views.djangoHello), path('index/',views.index), path('login/',views.login), ]
在應用下創(chuàng)建templates 文件夾
并創(chuàng)建html文件 index.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>FirstWeb</title> </head> <body> <h2>信息注冊</h2> <!-- action="/login/" 這里是 提交后訪問的路徑,因此 要在 urls 添加改路徑 --> <form action="/login/" method="post"> {% csrf_token %} 用戶名:<input type="text" name="username" id="usn"><br> 密 碼:<input type="password" name="password" id="pwd"><br> <input type="submit" value="注冊"> </form> </body> </html>
login.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>FirstWeb-登錄</title> </head> <body> <h2>您好,您已注冊成功!</h2> </body> </html>
介紹一下添加的common文件
添加一個數據庫封裝的類。
# FileName : DBHandle.py # Author : Adil # DateTime : 2018/11/29 2:03 PM # SoftWare : PyCharm import pymysql # username : adil # password : helloyyj class DataBaseHandle(object): ''' 定義一個 MySQL 操作類''' def __init__(self,host,username,password,database,port): '''初始化數據庫信息并創(chuàng)建數據庫連接''' # 下面的賦值其實可以省略,connect 時 直接使用形參即可 self.host = host self.username = username self.password = password self.database = database self.port = port self.db = pymysql.connect(self.host,self.username,self.password,self.database,self.port,charset='utf8') # 這里 注釋連接的方法,是為了 實例化對象時,就創(chuàng)建連接。不許要單獨處理連接了。 # # def connDataBase(self): # ''' 數據庫連接 ''' # # self.db = pymysql.connect(self.host,self.username,self.password,self.port,self.database) # # # self.cursor = self.db.cursor() # # return self.db def insertDB(self,sql): ''' 插入數據庫操作 ''' self.cursor = self.db.cursor() try: # 執(zhí)行sql self.cursor.execute(sql) # tt = self.cursor.execute(sql) # 返回 插入數據 條數 可以根據 返回值 判定處理結果 # print(tt) self.db.commit() print('執(zhí)行成功') except: # 發(fā)生錯誤時回滾 self.db.rollback() print('執(zhí)行失敗') finally: self.cursor.close() def deleteDB(self,sql): ''' 操作數據庫數據刪除 ''' self.cursor = self.db.cursor() try: # 執(zhí)行sql self.cursor.execute(sql) # tt = self.cursor.execute(sql) # 返回 刪除數據 條數 可以根據 返回值 判定處理結果 # print(tt) self.db.commit() except: # 發(fā)生錯誤時回滾 self.db.rollback() finally: self.cursor.close() def updateDb(self,sql): ''' 更新數據庫操作 ''' self.cursor = self.db.cursor() try: # 執(zhí)行sql self.cursor.execute(sql) # tt = self.cursor.execute(sql) # 返回 更新數據 條數 可以根據 返回值 判定處理結果 # print(tt) self.db.commit() except: # 發(fā)生錯誤時回滾 self.db.rollback() finally: self.cursor.close() def selectDb(self,sql): ''' 數據庫查詢 ''' self.cursor = self.db.cursor() try: self.cursor.execute(sql) # 返回 查詢數據 條數 可以根據 返回值 判定處理結果 data = self.cursor.fetchall() # 返回所有記錄列表 print(data) # 結果遍歷 for row in data: sid = row[0] name = row[1] # 遍歷打印結果 print('sid = %s, name = %s'%(sid,name)) except: print('Error: unable to fecth data') finally: self.cursor.close() def closeDb(self): ''' 數據庫連接關閉 ''' self.db.close() if __name__ == '__main__': DbHandle = DataBaseHandle('127.0.0.1','username','password','dbname',3306) sql = "insert into JdwSpider(image_name,image_url,Spider_time) values ('%s','%s','%s')" % ( '1', '2', '2018-12-04 15:25:21') DbHandle.insertDB(sql) # DbHandle.insertDB('insert into test(name) values ("%s")'%('FuHongXue')) # DbHandle.insertDB('insert into test(name) values ("%s")'%('FuHongXue')) # DbHandle.selectDb('select * from test') # DbHandle.updateDb('update test set name = "%s" where sid = "%d"' %('YeKai',22)) # DbHandle.selectDb('select * from test') # DbHandle.insertDB('insert into test(name) values ("%s")'%('LiXunHuan')) # DbHandle.deleteDB('delete from test where sid > "%d"' %(25)) # DbHandle.selectDb('select * from test') DbHandle.closeDb()
以上代碼實現(xiàn)了一個簡單的注冊頁面,并將注冊信息存放到數據庫表中。
啟動項目演示
打開瀏覽器輸入url:http://127.0.0.1:8000/index/
點擊注冊提交按鈕,頁面跳轉如下
查看數據庫表,可以看到新增的用戶信息。
以上是“Python中Django如何實現(xiàn)簡單注冊功能”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注創(chuàng)新互聯(lián)成都網站設計公司行業(yè)資訊頻道!
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
當前標題:Python中Django如何實現(xiàn)簡單注冊功能-創(chuàng)新互聯(lián)
分享網址:http://www.rwnh.cn/article48/joghp.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供網站建設、響應式網站、用戶體驗、外貿網站建設、服務器托管、定制開發(fā)
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內容