python:前端(HTML)+后端(Django)+数据库(MySQL)
1、创建一个html文件用于简单的网页注册demo
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>注册</title> </head> <body>
<!--表单域,action用于将获取的到信息提交到后台地址-->
<form action="save" method="get"> <span> 用户:<input type="text" name="username"> </span> <br> <span> 密码:<input type="password" name="password"> </span> <br> <span> <input type="submit" name="submit1" value="注册"> </span> </form> </body> </html>
2、创建一个html文件用于简单的网页登录demo
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登录</title> </head> <body> <form action="query" method="get"> <span> 用户:<input type="text" name="username"> </span> <br> <span> 密码:<input type="password" name="password"> </span> <br> <span> <input type="submit" name="submit1" value="登录"> </span> </form> </body> </html>
3、创建一个Django项目,并将刚才的两个html文件放入template模板文件夹中
4、打开Django项目下默认生成的urls.py文件,进行一些业务功能的编辑
"""Django01 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 django.shortcuts import render from django.shortcuts import HttpResponse import pymysql #登录页面 def login(request): #指定要访问的页面,render的功能:讲请求的页面结果提交给客户端 return render(request,'login.html') #注册页面 def regiter(request): return render(request,'regiter.html') #定义一个函数,用来保存注册的数据 def save(request): has_regiter = 0#用来记录当前账号是否已存在,0:不存在 1:已存在 a = request.GET#获取get()请求 #print(a) #通过get()请求获取前段提交的数据 userName = a.get('username') passWord = a.get('password') #print(userName,passWord) #连接数据库 db = pymysql.connect('127.0.0.1','root','123','db2') #创建游标 cursor = db.cursor() #SQL语句 sql1 = 'select * from user1' #执行SQL语句 cursor.execute(sql1) #查询到所有的数据存储到all_users中 all_users = cursor.fetchall() i = 0 while i < len(all_users): if userName in all_users[i]: ##表示该账号已经存在 has_regiter = 1 i += 1 if has_regiter == 0: # 将用户名与密码插入到数据库中 sql2 = 'insert into user1(username,password) values(%s,%s)' cursor.execute(sql2,(userName,passWord)) db.commit() cursor.close() db.close() return HttpResponse('注册成功') else: cursor.close() db.close() return HttpResponse('该账号已存在') def query(request): a = request.GET userName = a.get('username') passWord = a.get('password') user_tup = (userName,passWord) db = pymysql.connect('127.0.0.1','root','123','db2') cursor = db.cursor() sql = 'select * from user1' cursor.execute(sql) all_users = cursor.fetchall() cursor.close() db.close() has_user = 0 i = 0 while i < len(all_users): if user_tup == all_users[i]: has_user = 1 i += 1 if has_user == 1: return HttpResponse('登录成功') else: return HttpResponse('用户名或密码有误') urlpatterns = [ path('admin/', admin.site.urls),#系统默认创建的 path('login/',login),#用于打开登录页面 path('regiter/',regiter),#用于打开注册页面 path('regiter/save',save),#输入用户名密码后交给后台save函数处理 path('login/query',query)#输入用户名密码后交给后台query函数处理 ]
5、运行整个Django项目,访问相应的url
数据库数据情况:
后台状态信息
再次查看数据库数据:
6、再次进行登录操作,打开登录的url
测试一个用户名错误情况:
所思及所学
学而不思则罔,思而不学则殆!