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. canvas与svg

    canvas与svg都是用于在网页上绘制图形(位图). canvas是HTML5新出来的一个标签,用来定义一块画图的区域(canvas本身没有绘制能力),用JavaScript来画图,可以绘制路径.矩 ...

  2. 在Linux(Debian)环境下搭建并运行GPU

    首先通过以下命令查看是否GPU驱动成功: 注意:需要在bash终端输入 import tensorflow as tf hello = tf.constant('Hello, TensorFlow!' ...

  3. UVA - 12298 Super Poker II NTT

    UVA - 12298 Super Poker II NTT 链接 Vjudge 思路 暴力开个桶,然后统计,不过会T,用ntt或者fft,ntt用个大模数就行了,百度搜索"NTT大模数&q ...

  4. 调参贝叶斯优化(BayesianOptimization)

    from sklearn.datasets import make_classification from sklearn.model_selection import cross_val_score ...

  5. django 开发笔记1

    1.布置到云服务器 使用 python3 manage.py runserver提示错误,需要将settings.py 中 ALLOWED_HOSTS 字典中加入 localhost;0.0.0.0; ...

  6. NGUI实现滑动屏幕的时候,进行环形旋转

    在滑动屏幕的时候,上图中的内容饶圆中心旋转,并且箭头的方向保持不变 每个Item上挂载的脚本: using UnityEngine; public class ItemTest : MonoBehav ...

  7. lua---研究 c-api

    c-api 参考手册:http://www.leeon.me/a/lua-c-api-manual

  8. Effective java 系列之更优雅的关闭资源-try-with-resources

    背景: 在Java编程过程中,如果打开了外部资源(文件.数据库连接.网络连接等),我们必须在这些外部资源使用完毕后,手动关闭它们.因为外部资源不由JVM管理,无法享用JVM的垃圾回收机制,如果我们不在 ...

  9. Python第三方库的安装 --Python3

    1.使用安装包管理工具安装:easy_install .pip/pip3 easy_install:easy_install是由PEAK(Python Enterprise Application K ...

  10. Android 正则表达式实例

    editText正则表达式的使用 检查输入是否符合规则 import Android.app.Activity; import android.os.Bundle; import android.vi ...