圖書管理系統(一):出版社列表增長、刪除和編輯

  • 步驟一:前提準備
  • 步驟二:建立出版社列表
  • 步驟三:增長出版社
  • 步驟四:刪除出版社
  • 步驟五:編輯出版社
 
步驟一:前提準備

1.安裝django:    pip install -i http://pypi.douban.com/simple/ django==1.11.11
2.建立一個django項目:    Django-admin startproject 項目名(mysite)
3.建立一個app:    python3 manage.py startapp 名字(publisher)
4.對項目進行初始化設置:
#settings.py
 
INSTALLED_APPS = [
    'app_publisher'    #把新建app的名字加進去
]
 
MIDDLEWARE = [
    #'django.middleware.csrf.CsrfViewMiddleware',    #註釋掉這一行
]
 
TEMPLATES = [
{
    'DIRS': [os.path.join(BASE_DIR, 'templates')],    #在和html相關的設置中,加入路徑,base_dir是項目的根目錄,templates是專門設置的放html的文檔
}
]
 
#修改數據庫的設置
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'HOST': '127.0.0.1',
        'PORT': 3306,
        'NAME': 'mysite',    #新建的數據庫
        'USER': 'root',
        'PASSWORD': 'ren666666'
    }
}
 
STATIC_URL = '/static/'
#在這裏添加css,js等靜態文件的目錄
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]
#app_publisher/models.py
 
#給數據庫publisher添加表id和name
class publisher(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(null=False, max_length=20)
#mysite/mysite/__init__.py
 
#啓動pymysql,告訴django用pymysql來代替默認的MySQLdb
import pymysql
pymysql.install_as_MySQLdb()
5.數據庫設置完成後,運行命令
python3 manage.py makemigrations    #用小本本將models.py裏面的改動記錄下來,記錄到app1/migrations文件夾下面
python3 manage.py migrate    #把改動翻譯成SQL語句去數據庫執行
至此,準備工做完成!
 
步驟二:建立出版社列表

1.寫入函數:
#mysite/urls.py
 
from app_publisher import views
 
urlpatterns = [
    url(r'publisher_list', views.publisher_list),
]
#app_publisher/views.py
 
from django.shortcuts import render, HttpResponse, redirect
from app_publisher import models
 
def publisher_list(request):
    ret = models.publisher.objects.all().order_by('id')    #獲取數據庫中的數據,並賦值爲ret
    return render(request, 'publisher_list.html', {'publisher_list': ret})    把ret(數據庫中的數據)賦值給publisher_list,而後將publisher_list傳到publisher_list.html中
2.這個時候就須要一個顯示列表的html網頁了
#templates.py
 
<!DOCTYPE html>
<html>
<head>
    <title>出版社列表</title>
</head>
<body>
    <a href="/add_publisher/">增長出版社</a>
    <table border='1'>
        <tr>
            <th>序號</th>
            <th>id</th>
            <th>出版社</th>
        </tr>
        #固定格式,for循環來獲取傳入的數據庫中的數據
         {% for publisher in publisher_list %}
        <tr>
            <td>{{ forloop.counter }}</td>    #得到自動的序列
            <td>{{ publisher.id }}</td>
            <td>{{ publisher.name }}</td>
        </tr>
        {% endfor %}
    </table>
</body>
</html>
 
步驟三:設置增長出版社的功能

1.寫入函數
urlpatterns = [
     url(r'^add_publisher', views .add_publisher),
]
def add_publisher(request):
     error_msg = ''
    if request.method == 'POST':
        ret = request.POST.get('add_name', None)
        if ret:
             models.publisher.objects.create(name=ret).save()    #將增長的數據保存到數據庫中
            return redirect('/publisher_list/')
        else:
             error_msg = '出版社名字不能爲空!'
    return render(request, 'add_publisher.html', {'error': error_msg})
2.寫入頁面:
#templates/add_publisher.html
 
<!DOCTYPE html>
<html>
<head>
    <title>增長出版社</title>
</head>
<body>
    <form action=" /add_publisher/ " method=" post ">
        <input type="text" name="add_name"><br>
        <input type=" submit " value="提交">
    </form>
    <p> {{ error }} </p>
</body>
</html>
 
效果演示:

publisher_list:

 

add_publisher:
 

 

publisher_list:
 
 

 


步驟四:刪除出版社
1.寫入函數:
url(r'^delete_publisher/', views.delete_publisher),
def delete_publisher(request):
    ret = request.GET.get('id', None)
    if ret:
        models.publisher.objects. get(id=ret). delete()
        return redirect('/publisher_list/')
    else:
        return HttpResponse('要刪除的數據不存在')
2.在publisher_list.html文件中增長刪除內容:
<!DOCTYPE html>
<html>
<head>
    <title>出版社列表</title>
</head>
<body>
    <a href="/add_publisher/">增長出版社</a>
    <table border='1'>
        <tr>
            <th>序號</th>
            <th>id</th>
            <th>出版社</th>
             <th>操做</th>
        </tr>
        {% for publisher in publisher_list %}
        <tr>
            <td>{{ forloop.counter }}</td>
            <td>{{ publisher.id }}</td>
            <td>{{ publisher.name }}</td>
            <td>< a href=' /delete_publisher/?id={{ publisher.id }}'>刪除</ a></td>
        </tr>
        {% endfor %}
    </table>
</body>
</html>
 
步驟五:編輯出版社

1.寫入函數:
url(r'^edit_publisher/', views.edit_publisher),
def edit_publisher(request):
        ret = request.GET.get('id', None)
        if ret:
            edit_data = models.publisher.objects.get(id=ret)
            return render(request, 'edit_publisher.html', {'publisher': edit_data})
        new_name = request.POST.get('edit_name', None)
        ret = request.POST.get('id', None)
        if ret:
            publisher = models.publisher.objects.get(id=ret)
            publisher.name = new_name
            publisher.save()
            return redirect('/publisher_list/')
2.在publisher_list.html文件中增長修改內容:
<td><a href='/edit_publisher/?id={{ publisher.id }}'>修改</a></td>
3.寫edit_publisher.html文件:
<!DOCTYPE html>
<html>
<head>
    <title>修改出版社</title>
</head>
<body>
    <h1>修改出版社</h1>
    <form action="/edit_publisher/" method="post">
        <input type="text" name='id' value="{{ publisher.id}}" style='display:none'>
        <input type="text" name="edit_name" value="{{ publisher.name }}">
 
        <input type="submit" value="提交">
    </form>
    <p>{{ error }}</p>
</body>
</html>
 
效果演示:

publisher_list:
 

 

edit_publisher:
 

 

publisher_list:
 
相關文章
相關標籤/搜索