数据库增删改查
Posted 小学森也要学编程
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据库增删改查相关的知识,希望对你有一定的参考价值。
下载pycharm,Django的具体学习过程在自强学堂上有。
①新建python项目,在命令行cmd中输入命令
django-admin.py startproject learn_models
进入learn_models文件夹,新建一个名为lzhc的app
cd learn_models
python manage.py startapp lzhc
把 lzhc加入到settings.py中的INSTALLED_APPS中
INSTALLED_APPS = ( ‘django.contrib.admin‘, ‘django.contrib.auth‘, ‘django.contrib.contenttypes‘, ‘django.contrib.sessions‘, ‘django.contrib.messages‘, ‘django.contrib.staticfiles‘, ‘lzhc‘, )
同步所有数据库
python manage.py makemigrations
python manage.py migrate
②models.py
from django.db import models class Article(models.Model): title = models.CharField(max_length=20) author = models.CharField(max_length=20) def _str_(self): return self.title
③项目urls.py
from django.conf.urls import include, url from django.contrib import admin urlpatterns = [ url(r‘^admin/‘, include(admin.site.urls)), url(r‘^lzhc/‘, include(‘lzhc.urls‘)), ]
④lzhc文件夹下创建urls.py
from django.conf.urls import url from . import views urlpatterns = [ url(r‘^query/$‘, views.queryAll,name=‘query‘), url(r‘^delete/$‘, views.delByID,name=‘detele‘), url(r‘^beginadd/$‘, views.addByID,name=‘beginadd‘), url(r‘^add/$‘, views.add,name=‘add‘), url(r‘^update/$‘, views.update,name=‘update‘), url(r‘^beginupdate/$‘,views.updateByID,name=‘beginupdate‘) ]
⑤写出增删改查的方法。修改views.py
from django.shortcuts import render from django.shortcuts import render_to_response from .models import Article from django.http import HttpResponse from django.http import HttpResponseRedirect def queryAll(request): b=Article.objects.all() return render_to_response(‘queryAll.html‘,{‘data‘:b}) def delByID(request): id = request.GET[‘id‘]; bb = Article.objects.get(id=id) bb.delete() return HttpResponseRedirect("http://127.0.0.1:8000/lzhc/query") def addByID(request): id = request.POST[‘id‘] title = request.POST[‘title‘] author = request.POST[‘author‘] st = Article() if len(id) > 0: st.id=id; st.title=title st.author=author st.save() return HttpResponseRedirect("http://127.0.0.1:8000/lzhc/query") def add(request): return render_to_response(‘add.html‘) def update(request): i=request.GET[‘id‘]; b=Article.objects.get(id=i) return render_to_response(‘update.html‘,{‘data‘:b}) def updateByID(request): id = request.POST[‘id‘] title = request.POST[‘title‘] author = request.POST[‘author‘] st = Article() st.id = id st.title = title st.author = author st.save() return HttpResponseRedirect("http://127.0.0.1:8000/lzhc/query")
⑥admin.py
from django.contrib import admin from .models import Article admin.site.register(Article)
⑦在lzhc下新建templates文件夹,文件夹下存放html文件
add.html
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>添加数据,提交form表单</title> </head> <body> <form action="http://127.0.0.1:8000/lzhc/beginadd/" method="post" align="center"> <input name="id" type="hidden" value="" ><br/> 请输入标题:<input name="title" type="text" value=""><br/> 请输入作者: <input name="author" type="text" value=""><br/> <input type="submit" value="提交" > </form> </body> </html>
queryAll.html
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>数据展示平台</title> </head> <body> <table border="1" align="center"> <tr> <td>编号</td> <td>标题</td> <td>作者</td> <td>操作</td> </tr> {% for d in data %} <tr> <td>{{ d.id }}</td><td>{{ d.title }}</td><td>{{ d.author }}</td> <td> <a href="http://127.0.0.1:8000/lzhc/delete?id={{d.id}}">删除</a> <a href="http://127.0.0.1:8000/lzhc/add">添加</a> <a href="http://127.0.0.1:8000/lzhc/update?id={{d.id}}">修改</a> </td> </tr> {% endfor %} </table> </body> </html>
update.html
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>修改个人信息</title> </head> <body> <form action="http://127.0.0.1:8000/lzhc/beginupdate/" method="post" > <input type="hidden" name="id" value="{{ data.id }}" > 标题:<input name="title" type="text" value="{{ data.title }}"><br/> 作者:<input name="author" type="text" value="{{ data.author }}"><br/> <input type="submit" value="保存"/> </form> </body> </html>
⑧启动服务器。在当前面目录下,执行命令:
python manage.py runserver
⑨在浏览器上访问,输入网址:
http://127.0.0.1:8000/lzhc/add/
以上是关于数据库增删改查的主要内容,如果未能解决你的问题,请参考以下文章