1. 程式人生 > >數據庫增刪改查

數據庫增刪改查

min objects 個人 cfile submit spa shortcut span files

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

數據庫增刪改查