欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python Django 實(shí)現(xiàn)簡(jiǎn)單注冊(cè)功能過(guò)程詳解

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

項(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • win10下opencv-python特定版本手動(dòng)安裝與pip自動(dòng)安裝教程

    win10下opencv-python特定版本手動(dòng)安裝與pip自動(dòng)安裝教程

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

    Pytorch 使用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-06
  • python的getattr和getattribute攔截內(nèi)置操作實(shí)現(xiàn)

    python的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-01
  • pycharm軟件實(shí)現(xiàn)設(shè)置自動(dòng)保存操作

    pycharm軟件實(shí)現(xiàn)設(shè)置自動(dòng)保存操作

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

    Python?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)

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

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

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

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

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

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

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

    Python線程threading(Thread類(lèi))

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

最新評(píng)論