1、settings.py
激活应用
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp'#激活应用
]
连接数据库,这里是SQL server
##连接数据库
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'bishe',
'USER':'sa',
'PASSWORD':'root',
'HOST':'localhost',
'PORT':'',
'OPTIONS': {
'driver':'SQL Server Native Client 11.0',
'MARS_Connection': True,
}
}
}
注释掉这句话
记得改位置
2.在myapp文件夹下创建文件夹Templates
用来存html文件, index.html, login.html,register.html,三个文件后边展示代码
在项目的urls.py文件中加入两句话
from django.contrib import admin
from django.urls import path
from django.urls import path, include #1
urlpatterns = [
path('admin/', admin.site.urls),
path('myapp/', include('myapp.urls')) #2
]
在myapp文件夹里新建urls.py文件
输入代码:
from django.urls import path
from . import views
urlpatterns = [
path('registe/', views.register,name='register'),
path('log/', views.login,name='login'),
]
在myapp里的models.py文件中输入类:
from django.db import models
# Create your models here.
class User(models.Model):
name = models.CharField(max_length=50)
password = models.CharField(max_length=50)
telephone = models.CharField(max_length=50,primary_key=True)
email = models.CharField(max_length=50)
#定义默认输出格式
def __str__(self):
return "%s,%s,%s,%s"%(self.name,self.password,self.telephone,self.email)
#自定义对应的表名
class Meta:
db_table="user"
在Myapp里的views.py中输入代码
from django.shortcuts import redirect, render
from django.http import HttpResponse
from myapp.models import User
from django.contrib.auth import authenticate,login,logout
from django.contrib.auth.backends import ModelBackend
# Create your views here.
def index(request):
login_msg="恭喜!登录成功"
return render(request,'index.html',{'login_msg':login_msg})
def register(request):
if request.method=='POST':
userName=request.POST.get('userName')
userTelephone=request.POST.get('userTel')
userEmail=request.POST.get('userEmail')
userPassword=request.POST.get('userPassword')
userRePassword=request.POST.get('userRePassword')
try:
user=User.objects.get(telephone=userTelephone)
if user:
msg="用户已存在"
return render(request,'register.html',{'msg':msg})
except:
if userPassword!=userRePassword:
error_msg="密码不一致"
return render(request,'register.html',{'error_msg':error_msg})
else:
register=User()
register.name=userName
register.telephone=userTelephone
register.password=userPassword
register.emaild=userEmail
register.save()
return redirect('/myapp/log/')
else:
return render(request,'register.html')
def login(request):
if request.method=='POST':
userTelephone=request.POST.get('usertel')
userPassword=request.POST.get('userpassword')
if User.objects.get(telephone=userTelephone):
if User.objects.get(telephone=userTelephone).password==userPassword:
msg='恭喜登录成功!'
return render(request,'index.html',{'login_msg':msg})
else:
msg='手机号或密码错误'
return render(request,'login.html',locals(),{'msg':msg})
else:
msg='用户不存在,请先注册!'
return render(request,'login.html',locals(),{'msg':msg})
else:
return render(request,'login.html')
3.html页面
1.login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>登录</title>
</head>
<style>
.login{
text-align:center
}
</style>
<body>
<div class="login">
<h1>用户登录</h1>
<form action="/myapp/log/" method="post">
<div style="color:red">{{msg}}</div>
<p>
<label for="">手机号:</label>
<input type="tel" placeholder="手机号" name="usertel">
</p>
<p>
<label for="">密码:</label>
<input type="password" placeholder="密码" name="userpassword">
</p>
<p>
<input type="submit" value="登录">
</p>
<a href="/myapp/registe">注册</a>
</form>
</div>
</body>
</html>
2.register.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>注册</title>
<style>
.regi{
text-align:center
}
</style>
</head>
<body>
<div class="regi">
<h1>用户注册</h1>
<form action="/myapp/registe/" method="post">
<p>
<label for="">昵称</label>
<input type="text" name="userName">
</p>
<p>
<label for="">手机号</label>
<input type="tel" name="userTel">
<div style="color:red">{{msg}}</div>
</p>
<p>
<label for="">邮箱</label>
<input type="email" name="userEmail">
</p>
<p>
<lable for="">密码</lable>
<input type="password" name="userPassword">
</p>
<p>
<label for="">确认密码</label>
<input type="password" name="userRePassword">
<div style="color:red">{{error_msg}}</div>
</p>
<p>
<input type="submit" value="注册">
</p>
</form>
</div>
</body>
</html>
3.index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>登录成功</title>
<style>
div{
text-align:center
}
</style>
</head>
<body>
<div style="color:red"><h1>{{login_msg}}</h1></div>
</body>
</html>
4.测试
win+r ,cmd ,进到manage.py路径中
输入python manage.py runserver
C:\Users\86182\OneDrive\桌面\毕业设计\vscode\Liyanlin_BiShe>python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
March 17, 2023 - 14:16:16
Django version 2.1.15, using settings 'Liyanlin_BiShe.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
打开浏览器,进入http://127.0.0.1:8000/myapp/log/
进入登录页面
点击下方注册,可进行注册