https://www.jianshu.com/p/bd0af02e59ba

一、页面展示

做一个简单的数据库交换的练习案例

 
页面.png

二、创建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;
width: 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;
width: 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;
width: 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
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

Django_简单的数据库交互案例的更多相关文章

  1. 【JSP】JSP与oracle数据库交互案例

    ************************************************************************ ****原文:blog.csdn.net/clark_ ...

  2. ORM初级实战简单的数据库交互

    setting.py中: """ Django settings for untitled3 project. Generated by 'django-admin st ...

  3. OAF_JDBC系列1 - 数据库交互取值方式(案例)

    2014-06-15 Created By BaoXinjian

  4. java实现简单的数据库的增删查改,并布局交互界面

        一.系统简介 1.1.简介  本系统提供了学生信息管理中常见的基本功能,主要包括管理员.管理员的主要功能有对学生信息进行增加.删除.修改.查找等操作,对信息进行管理,对信息进行修改.查找等操作 ...

  5. ajax交互案例

    数据交互是前端很重要的一部分,静态页是基础,而交互才是网页的精髓.交互又分为人机交互和前后端数据交互,现阶段的互联网下,大部分的网站都要进行前后端数据交互,如何交互呢?交互的流程大概就是前端发送数据给 ...

  6. 利用NHibernate与MySQL数据库交互

    本文章使用Visual Studio作为开发工具,并建立在已经安装MySQL数据库的前提. NHibernate是一个面向.NET环境的对象/关系数据库映射工具.官网:http://nhibernat ...

  7. .NET应用程序与数据库交互的若干问题

    我们知道,在应用程序中与数据库进行交互是一个比较耗时的过程,首先应用程序需要与应用程序建立连接,然后将请求发送到数据库,数据库执行操作,然后将结果集返回.所以在程序中,要尽量晚的与数据库建立连接,并且 ...

  8. C#如何定制Excel界面并实现与数据库交互

    Excel是微软办公套装软件的一个重要的组成部分,它可以进行各种数据的处理.统计分析和辅助决策操作,广泛地应用于管理.统计财经.金融等众多领域.(另外,Excel还是伦敦一所会展中心的名称)..NET ...

  9. 【转载】Ssh整合开发介绍和简单的登入案例实现

    Ssh整合开发介绍和简单的登入案例实现 Ssh整合开发介绍和简单的登入案例实现 一  介绍: Ssh是strtus2-2.3.1.2+ spring-2.5.6+hibernate-3.6.8整合的开 ...

随机推荐

  1. Android中的数据储存

    数据的储存是一个十分重要的功能,它涉及到各种类型的数据,各种的储存方式,今天就接触了Android中数据储存的简单应用,有一种方式是可以将存入的数据原封不动的存储起来,这里要用到openfileout ...

  2. HDU 4417 Super Mario(主席树 区间不超过k的个数)题解

    题意:问区间内不超过k的个数 思路:显然主席树,把所有的值离散化一下,然后主席树求一下小于等于k有几个就行.注意,他给你的k不一定包含在数组里,所以问题中的询问一起离散化. 代码: #include& ...

  3. 【做题】CSA72G - MST and Rectangles——Borůvka&线段树

    原文链接 https://www.cnblogs.com/cly-none/p/CSA72G.html 题意:有一个\(n \times n\)的矩阵\(A\),\(m\)次操作,每次在\(A\)上三 ...

  4. 解决macOS因为它来自身份不明的开发者,不显示允许任何来源 –安装文件下载损坏问题

    打开时提示"已损坏,打不开.您应该将它移到废纸篓"或身份验证,因为它来自身份不明的开发者,和不显示允许任何来源,图片解锁和应用程序问题(如图片/application应用程序损坏, ...

  5. fiddler抓不到chrome浏览器的请求

    今天遇到一个非常尴尬的问题,接口在某种情况下会报错,此时前端会展示NAN之类的东西,由于复现不了,接口现在一直不报 错了,所以就让前端做了个友好提示, 当接口报错时,给个提示“请稍后重试” ,我要测试 ...

  6. java集合详解(附栈,队列)

    1 集合 1.1 为什么会出现集合框架 [1] 之前的数组作为容器时,不能自动拓容 [2] 数值在进行添加和删除操作时,需要开发者自己实现添加和删除. 1.2 Collection接口 1.2.1 C ...

  7. Confluence 6 在升级之前

    在这个指南中,我们将会与你一同对最新的 Confluence 站点在 Windows 或者 Linux 平台进行安装和更新. 如果你的 Confluence 安装实例是当前的许可证的话,那么对 Con ...

  8. oracle创建删除表空间

    create [undo|temporary] tablespace orcp datafile|tempfile 'E:\orcle\oracleBaseDir\oradata\orcp\orcp. ...

  9. 中国建设工程造价管理系统 http://zaojiasys.jianshe99.com/cecaopsys/

    建造师造价管理系统漏洞提示: 可以绕过,直接进入后台,为了安全起见,我就不多说了,. 里面的数据,从小学,中学,高中,大学,户口,电话,身份等, 很全, 本人没有破坏任何数据,

  10. MACE移植要求

    MACE支持Tensorflow的depth_to_space和space_to_depth,以及strided_slice算子. 其中depth_to_space可以用来无平滑地进行上采样. spa ...