简单版

展示出版社

# 展示出版社
    url(r'^publisher_list/', views.publisher_list),
def publisher_list(request):
    # 从数据库中查询所有出版社
    all_publisher = models.Publisher.objects.all()  # all可以查询所有
    # print(all_publisher, type(all_publisher))  # <QuerySet [<Publisher: Publisher object>, <Publisher: Publisher object>, <Publisher: Publisher object>]> <class 'django.db.models.query.QuerySet'>
    # for i in all_publisher:
    #     print(i, type(i))
    #     print(i.name)
    #     print(i.id)
    #     print(i.pk)
    # return HttpResponse('ok')
    return render(request, 'publisher_list.html', {'all_publisher': all_publisher})


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table border="1">
    <thead>
    <tr>
        <th>id</th>
        <th>名称</th>
    </tr>
    </thead>
    <tbody>
    {% for i in all_publisher %}
        <tr>
            <td>{{ i.id }}</td>
            <td>{{ i.name }}</td>
        </tr>
    {% endfor %}

    </tbody>
</table>
</body>
</html>

添加出版社

url(r'^add_publisher/', views.add_publisher),

简单版views.py

def add_publisher(request):
    # 处理POST请求
    if request.method == 'POST':
        # 获取提交的数据
        new_name = request.POST.get('new_name')
        if not new_name:
            return render(request, 'add_publisher.html',{'err_msg': '不能为空'})
        obj_list = models.Publisher.objects.filter(name=new_name)
        if obj_list:
            return render(request, 'add_publisher.html', {'err_msg': '数据已存在'})
        # 去数据库创建
        ret = models.Publisher.objects.create(name=new_name)
        # print(ret)
        return redirect('/publisher_list/')
    # 返回一个页面
    return render(request, 'add_publisher.html')

优化后:

def add_publisher(request):
    err_msg = ''
    # 处理POST请求
    if request.method == 'POST':
        # 获取提交的数据
        new_name = request.POST.get('new_name')
        if not new_name:
            err_msg = '不能为空'

        obj_list = models.Publisher.objects.filter(name=new_name)
        if obj_list:
            err_msg = '数据已存在'

        if new_name and not obj_list:
            # 去数据库创建
            ret = models.Publisher.objects.create(name=new_name)
            # print(ret)
            return redirect('/publisher_list/')
    # 返回一个页面
    return render(request, 'add_publisher.html',{'err_msg': err_msg})


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="" method="post">
    <p>
        出版社名称:<input type="text" name="new_name"> <span style="color: red">{{ err_msg }}</span>
    </p>
    <button>添加</button>
</form>

</body>
</html>

删除出版社

orm出版社_数据库

函数必须要返回一个http对象

url(r'^del_publisher/', views.del_publisher),
def del_publisher(request):
    # 获取要删除对象的id
    pk = request.GET.get('pk')

    if models.Publisher.objects.filter(pk=pk):
        # 去数据库进行删除
        obj = models.Publisher.objects.filter(pk=pk)   #get 查不到或者查到多个 都会报错
        obj.delete()
        # print(obj)  #<publisher obj -人民出版社>
        return redirect('/publisher_list/')

    return HttpResponse('数据不存在')
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table border="1">
    <thead>
    <tr>
        <th>序号</th>
        <th>id</th>
        <th>名称</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    {% for i in all_publisher %}
        <tr>
            <td>{{ forloop.counter }}</td>
            <td>{{ i.id }}</td>
            <td>{{ i.name }}</td>
        <td>
            <a href="/del_publisher/?pk={{ i.pk }}">删除</a>
        </td>
        </tr>
    {% endfor %}

    </tbody>
</table>
</body>
</html>


修改出版社

url(r'^edit_publisher/', views.edit_publisher),
def edit_publisher(request):
    # 获取要编辑对象的id
    pk = request.GET.get('pk')
    # 获取出版社对象
    obj = models.Publisher.objects.get(pk=pk)
    # print(obj)  #<publisher obj -内蒙古出版社>

    err_msg = ''
    if request.method == 'POST':
        # 获取提交的名字
        new_name = request.POST.get('new_name')

        if not new_name:
            err_msg = '不能为空'

        obj_list = models.Publisher.objects.filter(name=new_name)
        if obj_list:
            err_msg = '数据已存在'

        if new_name and not obj_list:
            # 名字不为空且数据库不存在这个数据
            obj.name = new_name  # 只是内存中修改了
            obj.save()  # 这样才提交到数据库中

            # 跳转到展示页面
            return redirect('/publisher_list/')

    return render(request, 'edit_publisher.html', {'obj': obj, 'err_msg': err_msg})

publisher_list:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<a href="/add_publisher/">添加</a>
<table border="1">
    <thead>
    <tr>
        <th>序号</th>
        <th>id</th>
        <th>名称</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    {% for i in all_publisher %}
        <tr>
            <td>{{ forloop.counter }}</td>
            <td>{{ i.id }}</td>
            <td>{{ i.name }}</td>
        <td>
            <a href="/del_publisher/?pk={{ i.pk }}">删除</a>
            <a href="/edit_publisher/?pk={{ i.pk }}">编辑</a>
        </td>
        </tr>
    {% endfor %}

    </tbody>
</table>
</body>
</html>

编辑:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="" method="post">
    <p>
        出版社名称:<input type="text" name="new_name" value="{{ obj.name }}"> <span style="color: red">{{ err_msg }}</span>
    </p>
    <button>修改</button>
</form>

</body>
</html>

使用bootstrap

urls.py

from app01 import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^publisher/', views.publisher),
    url(r'^add_publisher/', views.add_publisher),
    url(r'^edit_publisher/', views.edit_publisher),
]

models.py

class Publisher(models.Model):
    pid = models.AutoField(primary_key=True)  #Autofied自增主键
    name = models.CharField(max_length=32)  #varchar(32)

    def __str__(self):
        return '出版社对象-{}-{}'.format(self.pid,self.name)


views.py 

from django.shortcuts import render, redirect
from app01 import models


def publisher(request):
    # 获取到所有的出版社信息
    all_publisher = models.Publisher.objects.all()  # 对象列表

    return render(request, 'publisher.html', {'all_publisher': all_publisher})


def add_publisher(request):
    if request.method == 'POST':
        new_name = request.POST.get('new_name')
        obj = models.Publisher.objects.create(name=new_name)
        print(obj, type(obj))
        return redirect('/publisher/')

    return render(request, 'add_publisher.html')


def edit_publisher(request):
    pk = request.GET.get('pk')
    # 在数据库中获取操作
    obj = models.Publisher.objects.filter(pk=pk).first()
    # print(models.Publisher.objects.get(pk=pk))  # 出版社对象-1-沙河出版社
    # print(models.Publisher.objects.filter(pk=pk))  # <QuerySet [<Publisher: 出版社对象-1-沙河出版社>]>
    # print(models.Publisher.objects.filter(pk=pk)[0])  # <QuerySet [<Publisher: 出版社对象-1-沙河出版社>]>
    # print(models.Publisher.objects.filter(pk=pk).first())  # 获取不到就是None了,不会报错了

    # print(request.method,type(request.method))  #POST GET
    if request.method == 'POST':
        new_name = request.POST.get('new_name')

        obj.name = new_name  # 修改名字
        obj.save()
        return redirect('/publisher/')

    return render(request, 'edit_publisher.html', {'obj': obj})

publisher.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="/static/bootstrap/css/dashboard.css">
<body>

<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar"
                    aria-expanded="false" aria-controls="navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">Project name</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
            <ul class="nav navbar-nav navbar-right">
                <li><a href="#">Dashboard</a></li>
                <li><a href="#">Settings</a></li>
                <li><a href="#">Profile</a></li>
                <li><a href="#">Help</a></li>
            </ul>
            <form class="navbar-form navbar-right">
                <input type="text" class="form-control" placeholder="Search...">
            </form>
        </div>
    </div>
</nav>

<div class="container-fluid">
    <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">
            <ul class="nav nav-sidebar">
                <li class="active"><a href="/publisher/">出版社列表 <span class="sr-only">(current)</span></a></li>
                <li><a href="#">Reports</a></li>
                <li><a href="#">Analytics</a></li>
                <li><a href="#">Export</a></li>
            </ul>
        </div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">

            <h2 class="sub-header">出版社信息</h2>
            <div class="table-responsive">
                <a href="/add_publisher/" class="btn btn-primary btn-sm">添加</a>
                <table class="table table-striped table-bordered table-hover">
                    <thead>
                    <tr>
                        <th>序号</th>
                        <th>ID</th>
                        <th>名称</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for publisher in all_publisher %}
                        <tr>
                            <td>{{ forloop.counter }}</td>
                            <td>{{ publisher.pk }}</td>
                            <td>{{ publisher.name }}</td>
                            <td>
                                <a href="/edit_publisher/?pk={{ publisher.pk }}" class="btn btn-danger btn-sm">编辑</a>
                                <a href="" class="btn btn-primary btn-sm">删除</a>
                            </td>

                        </tr>
                    {% endfor %}

                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>


</body>
</html>

edit_publisher.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css">
<body>
<div class="container">
    <div class="row">
        <div class="col-lg-8 col-lg-offset-2" style="margin-top: 70px">
            <form class="form-horizontal" action="" method="post">
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label">名称</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="inputEmail3" name="new_name" placeholder="出版社名称" value="{{ obj.name }}">
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-default">提交</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
</body>
</html>

add_publisher.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css">
<body>
<div class="container">
    <div class="row">
        <div class="col-lg-8 col-lg-offset-2" style="margin-top: 70px">
            <form class="form-horizontal" action="" method="post">
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label">名称</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="inputEmail3" name="new_name" placeholder="出版社名称">
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-default">提交</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
</body>
</html>