django新手
1.没有注册账号,判断,提示该账户未注册,注册后写入数据库(mysql)
2.登录功能 --暂且提示登陆成功;空用户名提示、中文字符提示,不存在提示
登录模板层:
```python
```python
```html
<html>
<head>
<title>测试登录title>
<style>
form
{
width:100%;
height:100%;
margin-top: 0px;
margin-bottom: 20px;
background:#008B8B;
}
div
{
display:inline-block;
padding-top: 255px;
padding-bottom: 255px;
padding-left: 1px;
padding-right: 1px;
}
style>
head>
<body>
<center>
<form method="post" action="/login_action/">
<h1>登录测试h1>
<div>
用户名:<input name="username", type="text"><br>
密 码:<input name="password", type="password"><br>
{{ error }}br>
<button id="btn", type="submit">登录button>
div>
{% csrf_token %}
form>
center>
body>
html>
url
urlpatterns = [
path('admin/', admin.site.urls),
path('index/', views.index),
path('sign/', views.sign),
path('login/', views.login),
视图层
def login(request):
return render(request,'login.html',{'test':request.method})
def login_action(request):
if request.method =='POST':
user = request.POST.get('username')
password = request.POST.get('password')
if len(user)== 0 or len(password)==0:
# response = HttpResponseRedirect('/login_right/')
# response.set_cookie('user', user, 3600)
# request.session['user'] = user
# return response
return render(request, 'login.html',{'error':'用户名密码不能为空'})
else:
for _char in user:
if '\u4e00' <= _char <= '\u9fa5':
return render(request, 'sign.html',{'error':'用户名不能包含中文字符'})
user_list = models.Admin.objects.all()
logger.debug('开始判断用户:{}{}'.format(user,password))
for judge_user in user_list:
if judge_user.user == user and judge_user.password == password:
response = HttpResponseRedirect('/login_right/')
response.set_cookie('user', user, 3600)
models.Admin.objects.create(user = user,password=password)
request.session['user'] = user
return response
else:
continue
@login_required
def login_right(request):
user =request.COOKIES.get('user','')
# user =request.session.get('user','')
return render(request,'login_action.html',{'user':user}
模型层
class Student(models.Model):
"""
创建如下几个表的字段
"""
# 学号 primary_key=True: 该字段为主键
studentNum = models.CharField('学号', primary_key=True, max_length=15)
# 姓名 字符串 最大长度20
name = models.CharField('姓名', max_length=20)
# 年龄 整数 null=False, 表示该字段不能为空
age = models.IntegerField('年龄', null=False)
# 性别 布尔类型 默认True: 男生 False:女生
sex = models.BooleanField('性别', default=True)
# 手机 unique=True 该字段唯一
mobile = models.CharField('手机', unique=True, max_length=15)
# 创建时间 auto_now_add:只有在新增的时候才会生效
createTime = models.DateTimeField(auto_now_add=True)
# 修改时间 auto_now: 添加和修改都会改变时间
modifyTime = models.DateTimeField(auto_now=True)
hobby = models.CharField(max_length=13,default="study")
'''登录用户'''
class Admin(models.Model):
user =models.CharField(max_length=20)
password = models.CharField(max_length=20)
templates:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>For Studentstitle>
head>
<style>* {
margin: 0px;
padding: 0px;
}
.top {
height: 100px;
background-color: #041858;
color: honeydew;
}
.banner {
height: 41px;
background-color: #fcfcfc;
text-align: right;
line-height: 50px;
border-top: 3px solid #ff8500;
border-bottom: solid 1px #edeef0;
}
.bc {
display: inline-block;
width: 80px;
height: 40px;
font-size: 20px;
color: #4c4c4c;
line-height: 40px;
padding: 0px 20px;
text-decoration: none;
}
a.x {
display: block;
width: 140px;
height: 70px;
background-color: rgba(124, 119, 124, 0.993);
text-decoration: none;
text-align: center;
color: cornsilk;
line-height: 70px;
}
a.x:hover {
background-color: yellowgreen;
}style>
<body>
<div class="header">
<div class="top"><a style="text-decoration: none;line-height: 100px;font-family:STCaiyun; color: honeydew;font-size: 40px;" id="two">For Studentsa>div>
<div class="banner">
<a class="bc" href="/index">首页导航a>
<a class="bc" href="yingxiaojieshao.html">学习园地a>
<a class="bc" href="anli.html">在校实习a>
<a class="bc" href="#">学生管理a>
<a class="bc" href="#">其他板块a>
<a class="bc" href="guanyu.html">关于网站a>
div>
div>
<center>
<p>学生表信息p>
<form action="/student_query" , method="get">
<input name="student_query",type="text"><button id='btn',type="submit">查询button>
<br>
{% for item in msg %}
{{ item.studentNum }}{{ item.name }} {{ item.age }}
{% endfor %}
form>
<table border="1">
<tr>
<td>学号td>
<td>姓名td>
<td>年龄td>
<td>电话td>
<td>入学时间td>
<td>上次修改td>
<td>操作
<a href="/student_add" > 添加a>td>
tr>
{% for item in student_list %}
<tr>
<td>{{ item.studentNum }}td>
<td>{{ item.name }}td>
<td>{{ item.age }}td>
<td>{{ item.mobile }}td>
<td>{{ item.createTime|date:"Y-m-d H:i:s" }}td>
<td>{{ item.modifyTime|date:"Y-m-d H:i:s" }}td>
<td>
<a href="student_add" > 修改a>
<a href="/student_del/?studentNum={{ item.studentNum }}" > 删除a>td>
tr>
{% endfor %}
{{ msg}}
table>
center>
body>
html>
views
def student_info(request):
student_list =models.Student.objects.all()
# models.Admin.objects.filter(user='zhaowenyao').delete()
return render(request,'student_info.html',{'student_list':student_list})
views:
def student_query(request):
if request.method == 'GET':
studentNum =request.GET.get('student_query')
data_list = models.Student.objects.filter(studentNum =studentNum)
return render(request,'student_query.html',{'msg':data_list})
模板
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>For Studentstitle>
head>
<style>* {
margin: 0px;
padding: 0px;
}
.top {
height: 100px;
background-color: #041858;
color: honeydew;
}
.banner {
height: 41px;
background-color: #fcfcfc;
text-align: right;
line-height: 50px;
border-top: 3px solid #ff8500;
border-bottom: solid 1px #edeef0;
}
.bc {
display: inline-block;
width: 80px;
height: 40px;
font-size: 20px;
color: #4c4c4c;
line-height: 40px;
padding: 0px 20px;
text-decoration: none;
}
a.x {
display: block;
width: 140px;
height: 70px;
background-color: rgba(124, 119, 124, 0.993);
text-decoration: none;
text-align: center;
color: cornsilk;
line-height: 70px;
}
a.x:hover {
background-color: yellowgreen;
}style>
<body>
<div class="header">
<div class="top"><a style="text-decoration: none;line-height: 100px;font-family:STCaiyun; color: honeydew;font-size: 40px;" id="two">For Studentsa>div>
<div class="banner">
<a class="bc" href="/index">首页导航a>
<a class="bc" href="yingxiaojieshao.html">学习园地a>
<a class="bc" href="anli.html">在校实习a>
<a class="bc" href="#">学生管理a>
<a class="bc" href="#">其他板块a>
<a class="bc" href="guanyu.html">关于网站a>
div>
div>
<center>
<p>学生表信息p>
<form action="/student_query" , method="get">
<input name="student_query",type="text"><button id='btn',type="submit">查询button> <a href="/student_info">返回a>
<br>
form>
<table border="1">
<tr>
<td>学号td>
<td>姓名td>
<td>年龄td>
tr>
{% for item in msg %}
<tr>
<td>{{ item.studentNum }}td>
<td>{{ item.name }}td>
<td> {{ item.age }}td>
tr>
{% endfor %}
<br>
{%if item.studentNum == None %}
<p>学生信息不存在p>
{%endif%}
table>
center>
body>
html>
def student_del(request):
if request.method == 'GET':
studentNum = request.GET.get('studentNum')
models.Student.objects.filter(studentNum=studentNum).delete()
student_list = models.Student.objects.all()
# models.Admin.objects.filter(user='zhaowenyao').delete()
return render(request, 'student_info.html', {'student_list': student_list})
views
def student_add(request):
if request.method == 'GET':
return render(request,'student_add.html')
if request.method == 'POST':
name = request.POST.get('name')
no = request.POST.get('no')
age = request.POST.get('age')
mobile = request.POST.get('mobile')
logger.debug('开始判断用户:{}{}'.format(name, no))
if len(name) == 0 or len(no) == 0 or len(age)==0 or len(mobile)==0:
return render(request, 'student_add.html', {'error': '不能为空'})
else:
for _char in name:
if '\u4e00' <= _char <= '\u9fa5':
return render(request, 'sign.html', {'error': '用户名不能包含中文字符'})
models.Student.objects.create(studentNum=no, name=name, age=age, mobile=mobile)
student_list = models.Student.objects.all()
return render(request, 'student_info.html', {'student_list':student_list,'msg': '添加成功'})
模板
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>学生管理系统title>
head>
<style>* {
margin: 0px;
padding: 0px;
}
.top {
height: 100px;
background-color: #041858;
color: honeydew;
}
.banner {
height: 41px;
background-color: #fcfcfc;
text-align: right;
line-height: 50px;
border-top: 3px solid #ff8500;
border-bottom: solid 1px #edeef0;
}
.bc {
display: inline-block;
width: 80px;
height: 40px;
font-size: 20px;
color: #4c4c4c;
line-height: 40px;
padding: 0px 20px;
text-decoration: none;
}
a.x {
display: block;
width: 140px;
height: 70px;
background-color: rgba(124, 119, 124, 0.993);
text-decoration: none;
text-align: center;
color: cornsilk;
line-height: 70px;
}
a.x:hover {
background-color: yellowgreen;
}style>
<body>
<div class="header">
<div class="top"><a style="text-decoration: none;line-height: 100px;font-family:STCaiyun; color: honeydew;font-size: 40px;" id="two">学生管理系统a>div>
<div class="banner">
<a class="bc" href="http://127.0.0.1:8000/student_info.html">首页导航a>
<a class="bc" href="yingxiaojieshao.html">学习园地a>
<a class="bc" href="anli.html">在校实习a>
<a class="bc" href="#">学生管理a>
<a class="bc" href="#">其他板块a>
<a class="bc" href="guanyu.html">关于网站a>
div>
div>
<h2>添加学生表信息h2>
<form method="post" action="/student_add/">
<div>
姓 名:<input name="name", type="text"><br>
年 龄:<input name="age", type="text"><br>
学 号:<input name="no", type="no"><br>
电 话:<input name="mobile", type="mobile"><br>
<button id="btn", type="submit">添加button>
{% csrf_token %}
div>
form>
body>
html>