https://www.jianshu.com/p/bd0af02e59ba
一、页面展示
做一个简单的数据库交换的练习案例
二、创建mysql 表
(1)创建django
(2)创建app文件python mange.py startapp cmdb
(3)创建数据库,在project同名的配置的 init.py文件中配置mysql连接
import pymysql
pymysql.install_as_MySQLdb()
(4)在setting.py 中配置mysql 连接,找到DATABASES
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'testuser',
'USER':'root',
'PASSWORD':'root',
'HOST':'localhost',
'PORT': '3306',
}
}
(5)在setting文件下配置INSTALLED_APPS加入cmdb模块
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'cmdb',
]
(6)根据CODEFIRST创建表,在app models.py 创建类
from django.db import models
# Create your models here.
class user_info(models.Model):
username = models.CharField(max_length=32)
password = models.CharField(max_length=64)
(7)创建新的迁移策略文件python manage.py makemigrations
(8)生成数据库表python manage.py migrate
三、url 配置
(1)在project 文件的url配置,url分发,分发到指定的app
from django.conf.urls import url,include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'cmdb/', include('cmdb.urls'))
]
(2)在指定的app文件下创建urls.py文件
from django.conf.urls import url,include
from cmdb import views
urlpatterns = [
#登陆url
url(r'login', views.login),
#主界面展示url
url(r'index', views.index),
#展示所用户信息url
url(r'user_info', views.user_info),
#展示个人信息的url
url(r'user-(?P<nid>d+)',views.user_per),
#删除个人信息的url
url(r'delete-(?P<nid>d+)',views.user_delete),
]
四、views 层逻辑编写
(1)登陆主要用到了models.user_info.objects.filter(username=u, password=p).first()
def login(request):
if request.method == 'GET':
return render(request,'login.html',{'msg':''})
elif request.method == 'POST':
u = request.POST.get('user',None)
p = request.POST.get('pwd',None)
if u and p :
#select * from cmdb_user_info where username=u password=p
obj = models.user_info.objects.filter(username=u, password=p).first()
if obj:
#重定向到cmdb/index url 上,url分发到index方法上
return redirect('/cmdb/index')
else:
msg = '用户名密码错误'
return render(request,'login.html',{'msg':msg})
else:
return render(request, 'login.html', {'msg': ''})
(2)主页面展示
def index(request):
return render(request,'index_u.html')
(3)用户信息的增加/展示
主要用到了
#select * from cmdb_user_info
obj = models.user_info.objects.all()
#inster into cmdb_user_info(username,password) values(u,p)
models.user_info.objects.create(username=u,password=p)
def user_info(request):
if request.method == 'GET':
#select * from cmdb_user_info
obj = models.user_info.objects.all()
return render(request,'index.html',{'user':obj})
elif request.method == 'POST':
u = request.POST.get('user')
p = request.POST.get('pwd')
#inster into cmdb_user_info(username,password) values(u,p)
models.user_info.objects.create(username=u,password=p)
return redirect('/cmdb/user_info')
(4)删除
主要用到
#删除 delete from 表 where id=2
obj = models.user_info.objects.filter(id=nid).delete()
def user_delete(request, nid):
#删除 delete from 表 where id=2
obj = models.user_info.objects.filter(id=nid).delete()
return redirect('/cmdb/user_info')
四、templates
(1)login页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="{{ request.path_info }}" method="post">
<input type="text" name="user">
<input type="password" name="pwd">
<span>{{ msg }}</span>
<input type="submit">
</form>
</body>
</html>
(2)index 页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body{
margin: 0;
}
.header{
height: 48px;
background-color: aquamarine;
color: white;
}
.conleft{
position: absolute;
top: 48px;
200px;
left: 0;
bottom: 0;
background-color:chocolate;
}
.conright{
position: absolute;
left: 200px;
bottom: 0px;
right: 0px;
top: 48px;
overflow: auto;
background-color: burlywood;
}
</style>
</head>
<body>
<div class="header">欢迎</div>
<div class="con">
<div class="conleft">
<a href="/cmdb/user_info">用户管理</a>
</div>
<div class="conright">
</div>
</div>
<div></div>
</body>
</html>
(3)用户信息展示页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body{
margin: 0;
}
.header{
height: 48px;
background-color: aquamarine;
color: white;
}
.conleft{
position: absolute;
top: 48px;
200px;
left: 0;
bottom: 0;
background-color:chocolate;
}
.conright{
position: absolute;
left: 200px;
bottom: 0px;
right: 0px;
top: 48px;
overflow: auto;
background-color: burlywood;
}
</style>
</head>
<body>
<div class="header">欢迎</div>
<div class="con">
<div class="conleft">
<a href="/cmdb/user_info">用户管理</a>
</div>
<div class="conright">
<form action="{{ request.path_info}}" method="post">
<input type="text" name="user">
<input type="text" name="pwd">
<input type="submit" >
</form>
{% for i in user %}
<a href="/cmdb/user-{{ i.id }}">{{ i.username }}
<a href="/cmdb/delete-{{ i.id }}">删除</a>
<br>
{% endfor %}
</div>
</div>
<div></div>
</body>
</html>
(4)个人信息更改页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body{
margin: 0;
}
.header{
height: 48px;
background-color: aquamarine;
color: white;
}
.conleft{
position: absolute;
top: 48px;
200px;
left: 0;
bottom: 0;
background-color:chocolate;
}
.conright{
position: absolute;
left: 200px;
bottom: 0px;
right: 0px;
top: 48px;
overflow: auto;
background-color: burlywood;
}
</style>
</head>
<body>
<div class="header">欢迎</div>
<div class="con">
<div class="conleft">
<a href="/cmdb/user_info">用户管理</a>
</div>
<div class="conright">
<p>用户信息</p>
<form action="{{ request.path_info }}" method="post">
<input type="text" value="{{ i.username }}" name="user">-
<input type="text" value="{{ i.password }}" name="pwd">
<input type="submit">编辑</a>
</form>
</div>
</div>
<div></div>
</body>
</html>
作者:两点半的杂货铺
链接:https://www.jianshu.com/p/bd0af02e59ba
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。