亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python Django 實現(xiàn)簡單注冊功能過程詳解

 更新時間:2019年07月29日 15:27:12   作者:Blue·Sky  
這篇文章主要介紹了Python Django 實現(xiàn)簡單注冊功能過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

項目創(chuàng)建略,可參考Python Django Vue 項目創(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ù)據(jù)庫 連接
 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>
 <h1>信息注冊</h1>
 <!-- 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>
<h1>您好,您已注冊成功!</h1>
</body>
</html>

介紹一下添加的common文件

添加一個數(shù)據(jù)庫封裝的類。

# 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):
 '''初始化數(shù)據(jù)庫信息并創(chuàng)建數(shù)據(jù)庫連接'''
 # 下面的賦值其實可以省略,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):
 # ''' 數(shù)據(jù)庫連接 '''
 #
 # 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ù)庫操作 '''
 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ā)生錯誤時回滾
  self.db.rollback()
  print('執(zhí)行失敗')
 finally:
  self.cursor.close()
 def deleteDB(self,sql):
 ''' 操作數(shù)據(jù)庫數(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ā)生錯誤時回滾
  self.db.rollback()
 finally:
  self.cursor.close()

 def updateDb(self,sql):
 ''' 更新數(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ā)生錯誤時回滾
  self.db.rollback()
 finally:
  self.cursor.close()
 def selectDb(self,sql):
 ''' 數(shù)據(jù)庫查詢 '''
 self.cursor = self.db.cursor()
 try:
  self.cursor.execute(sql) # 返回 查詢數(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ù)庫連接關(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()

以上代碼實現(xiàn)了一個簡單的注冊頁面,并將注冊信息存放到數(shù)據(jù)庫表中。

啟動項目演示

打開瀏覽器輸入url:http://127.0.0.1:8000/index/

點擊注冊提交按鈕,頁面跳轉(zhuǎn)如下

查看數(shù)據(jù)庫表,可以看到新增的用戶信息。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • win10下opencv-python特定版本手動安裝與pip自動安裝教程

    win10下opencv-python特定版本手動安裝與pip自動安裝教程

    這篇文章主要介紹了win10下opencv-python特定版本手動安裝與pip自動安裝教程,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • Pytorch 使用CNN圖像分類的實現(xiàn)

    Pytorch 使用CNN圖像分類的實現(xiàn)

    這篇文章主要介紹了Pytorch 使用CNN圖像分類的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-06-06
  • python的getattr和getattribute攔截內(nèi)置操作實現(xiàn)

    python的getattr和getattribute攔截內(nèi)置操作實現(xiàn)

    在Python中,getattr和getattribute是用于動態(tài)屬性訪問和自定義屬性訪問行為的重要工具,本文主要介紹了python的getattr和getattribute攔截內(nèi)置操作實現(xiàn),具有一定的參考價值,感興趣的可以了解一下
    2024-01-01
  • pycharm軟件實現(xiàn)設置自動保存操作

    pycharm軟件實現(xiàn)設置自動保存操作

    這篇文章主要介紹了pycharm軟件實現(xiàn)設置自動保存操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • Python?Pandas實現(xiàn)將嵌套JSON數(shù)據(jù)轉(zhuǎn)換DataFrame

    Python?Pandas實現(xiàn)將嵌套JSON數(shù)據(jù)轉(zhuǎn)換DataFrame

    對于復雜的JSON數(shù)據(jù)進行分析時,通常的做法是將JSON數(shù)據(jù)結(jié)構(gòu)轉(zhuǎn)換為Pandas?DataFrame,所以本文就來看看將嵌套JSON數(shù)據(jù)轉(zhuǎn)換為Pandas?DataFrame的具體方法吧
    2024-01-01
  • 深度學習Tensorflow?2.4?完成遷移學習和模型微調(diào)

    深度學習Tensorflow?2.4?完成遷移學習和模型微調(diào)

    這篇文章主要為大家介紹了深度學習Tensorflow?2.4?完成遷移學習和模型微調(diào),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • 用OpenCV將視頻分解成單幀圖片,圖片合成視頻示例

    用OpenCV將視頻分解成單幀圖片,圖片合成視頻示例

    今天小編就為大家分享一篇用OpenCV將視頻分解成單幀圖片,圖片合成視頻示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • Python中__init__.py文件的作用詳解

    Python中__init__.py文件的作用詳解

    __init__.py 文件的作用是將文件夾變?yōu)橐粋€Python模塊,Python 中的每個模塊的包中,都有__init__.py 文件.這篇文章主要介紹了Python中__init__.py文件的作用詳解,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • Python?用戶輸入和字符串格式化示例詳解

    Python?用戶輸入和字符串格式化示例詳解

    Python 允許用戶輸入數(shù)據(jù)。這意味著我們可以向用戶詢問輸入,這篇文章主要介紹了Python?用戶輸入和字符串格式化指南,以下示例要求用戶輸入用戶名,并在輸入用戶名后將其打印在屏幕上,需要的朋友可以參考下
    2023-11-11
  • Python線程threading(Thread類)

    Python線程threading(Thread類)

    這篇文章主要介紹了Python線程threading(Thread類),線程是進程的組成部分,一個進程可以擁有多個線程,更多詳細內(nèi)容需要的朋友可以參考一下下面文章詳細內(nèi)容
    2022-07-07

最新評論