码迷,mamicode.com
首页 > 数据库 > 详细

数据库增删改查

时间:2017-10-28 12:39:17      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:def   enter   form   创建   post   pattern   content   input   访问   

下载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/

 

数据库增删改查

标签:def   enter   form   创建   post   pattern   content   input   访问   

原文地址:http://www.cnblogs.com/lzhc/p/7746349.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!