Python Django 實(shí)現(xiàn)簡(jiǎn)單注冊(cè)功能過(guò)程詳解
項(xiàng)目創(chuàng)建略,可參考Python Django Vue 項(xiàng)目創(chuàng)建。
目錄結(jié)構(gòu)如下
編輯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 # 實(shí)例化 數(shù)據(jù)庫(kù) 連接 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')
接下來(lái)編輯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), ]
在應(yīng)用下創(chuàng)建templates 文件夾
并創(chuàng)建html文件 index.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>FirstWeb</title> </head> <body> <h1>信息注冊(cè)</h1> <!-- action="/login/" 這里是 提交后訪問(wèn)的路徑,因此 要在 urls 添加改路徑 --> <form action="/login/" method="post"> {% csrf_token %} 用戶(hù)名:<input type="text" name="username" id="usn"><br> 密 碼:<input type="password" name="password" id="pwd"><br> <input type="submit" value="注冊(cè)"> </form> </body> </html>
login.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>FirstWeb-登錄</title> </head> <body> <h1>您好,您已注冊(cè)成功!</h1> </body> </html>
介紹一下添加的common文件
添加一個(gè)數(shù)據(jù)庫(kù)封裝的類(lèi)。
# FileName : DBHandle.py # Author : Adil # DateTime : 2018/11/29 2:03 PM # SoftWare : PyCharm import pymysql # username : adil # password : helloyyj class DataBaseHandle(object): ''' 定義一個(gè) MySQL 操作類(lèi)''' def __init__(self,host,username,password,database,port): '''初始化數(shù)據(jù)庫(kù)信息并創(chuàng)建數(shù)據(jù)庫(kù)連接''' # 下面的賦值其實(shí)可以省略,connect 時(shí) 直接使用形參即可 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') # 這里 注釋連接的方法,是為了 實(shí)例化對(duì)象時(shí),就創(chuàng)建連接。不許要單獨(dú)處理連接了。 # # def connDataBase(self): # ''' 數(shù)據(jù)庫(kù)連接 ''' # # 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): ''' 插入數(shù)據(jù)庫(kù)操作 ''' self.cursor = self.db.cursor() try: # 執(zhí)行sql self.cursor.execute(sql) # tt = self.cursor.execute(sql) # 返回 插入數(shù)據(jù) 條數(shù) 可以根據(jù) 返回值 判定處理結(jié)果 # print(tt) self.db.commit() print('執(zhí)行成功') except: # 發(fā)生錯(cuò)誤時(shí)回滾 self.db.rollback() print('執(zhí)行失敗') finally: self.cursor.close() def deleteDB(self,sql): ''' 操作數(shù)據(jù)庫(kù)數(shù)據(jù)刪除 ''' self.cursor = self.db.cursor() try: # 執(zhí)行sql self.cursor.execute(sql) # tt = self.cursor.execute(sql) # 返回 刪除數(shù)據(jù) 條數(shù) 可以根據(jù) 返回值 判定處理結(jié)果 # print(tt) self.db.commit() except: # 發(fā)生錯(cuò)誤時(shí)回滾 self.db.rollback() finally: self.cursor.close() def updateDb(self,sql): ''' 更新數(shù)據(jù)庫(kù)操作 ''' self.cursor = self.db.cursor() try: # 執(zhí)行sql self.cursor.execute(sql) # tt = self.cursor.execute(sql) # 返回 更新數(shù)據(jù) 條數(shù) 可以根據(jù) 返回值 判定處理結(jié)果 # print(tt) self.db.commit() except: # 發(fā)生錯(cuò)誤時(shí)回滾 self.db.rollback() finally: self.cursor.close() def selectDb(self,sql): ''' 數(shù)據(jù)庫(kù)查詢(xún) ''' self.cursor = self.db.cursor() try: self.cursor.execute(sql) # 返回 查詢(xún)數(shù)據(jù) 條數(shù) 可以根據(jù) 返回值 判定處理結(jié)果 data = self.cursor.fetchall() # 返回所有記錄列表 print(data) # 結(jié)果遍歷 for row in data: sid = row[0] name = row[1] # 遍歷打印結(jié)果 print('sid = %s, name = %s'%(sid,name)) except: print('Error: unable to fecth data') finally: self.cursor.close() def closeDb(self): ''' 數(shù)據(jù)庫(kù)連接關(guān)閉 ''' 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()
以上代碼實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的注冊(cè)頁(yè)面,并將注冊(cè)信息存放到數(shù)據(jù)庫(kù)表中。
啟動(dòng)項(xiàng)目演示
打開(kāi)瀏覽器輸入url:http://127.0.0.1:8000/index/
點(diǎn)擊注冊(cè)提交按鈕,頁(yè)面跳轉(zhuǎn)如下
查看數(shù)據(jù)庫(kù)表,可以看到新增的用戶(hù)信息。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- django+vue實(shí)現(xiàn)注冊(cè)登錄的示例代碼
- django注冊(cè)用郵箱發(fā)送驗(yàn)證碼的實(shí)現(xiàn)
- 通用的Django注冊(cè)功能模塊實(shí)現(xiàn)方法
- Django怎么在admin后臺(tái)注冊(cè)數(shù)據(jù)庫(kù)表
- Django用戶(hù)登錄與注冊(cè)系統(tǒng)的實(shí)現(xiàn)示例
- django 框架實(shí)現(xiàn)的用戶(hù)注冊(cè)、登錄、退出功能示例
- django實(shí)現(xiàn)用戶(hù)注冊(cè)實(shí)例講解
- Django實(shí)現(xiàn)auth模塊下的登錄注冊(cè)與注銷(xiāo)功能
- django的登錄注冊(cè)系統(tǒng)的示例代碼
- django 通過(guò)ajax完成郵箱用戶(hù)注冊(cè)、激活賬號(hào)的方法
- Django商城項(xiàng)目注冊(cè)功能的實(shí)現(xiàn)
相關(guān)文章
win10下opencv-python特定版本手動(dòng)安裝與pip自動(dòng)安裝教程
這篇文章主要介紹了win10下opencv-python特定版本手動(dòng)安裝與pip自動(dòng)安裝教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03Pytorch 使用CNN圖像分類(lèi)的實(shí)現(xiàn)
這篇文章主要介紹了Pytorch 使用CNN圖像分類(lèi)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06python的getattr和getattribute攔截內(nèi)置操作實(shí)現(xiàn)
在Python中,getattr和getattribute是用于動(dòng)態(tài)屬性訪問(wèn)和自定義屬性訪問(wèn)行為的重要工具,本文主要介紹了python的getattr和getattribute攔截內(nèi)置操作實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01pycharm軟件實(shí)現(xiàn)設(shè)置自動(dòng)保存操作
這篇文章主要介紹了pycharm軟件實(shí)現(xiàn)設(shè)置自動(dòng)保存操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06Python?Pandas實(shí)現(xiàn)將嵌套JSON數(shù)據(jù)轉(zhuǎn)換DataFrame
對(duì)于復(fù)雜的JSON數(shù)據(jù)進(jìn)行分析時(shí),通常的做法是將JSON數(shù)據(jù)結(jié)構(gòu)轉(zhuǎn)換為Pandas?DataFrame,所以本文就來(lái)看看將嵌套JSON數(shù)據(jù)轉(zhuǎn)換為Pandas?DataFrame的具體方法吧2024-01-01深度學(xué)習(xí)Tensorflow?2.4?完成遷移學(xué)習(xí)和模型微調(diào)
這篇文章主要為大家介紹了深度學(xué)習(xí)Tensorflow?2.4?完成遷移學(xué)習(xí)和模型微調(diào),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01Python線程threading(Thread類(lèi))
這篇文章主要介紹了Python線程threading(Thread類(lèi)),線程是進(jìn)程的組成部分,一個(gè)進(jìn)程可以擁有多個(gè)線程,更多詳細(xì)內(nèi)容需要的朋友可以參考一下下面文章詳細(xì)內(nèi)容2022-07-07