查询接口练习

查询接口练习

1.  创建一个A.html文件写入一个表格
2.  进入urls.py文件配置路由
Urlpatterns = [url(r’^A/$’,A)]
3.  进入views.py文件写入函数
a)  function A(request):
authors = Author.bojects.all()
return render(request,’A.html’,loacls())
4.  进入A.html文件循环:
{%for anthor in authors%}
    … …
    … …
{%endfor%}

.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <table border="1" >
        <tr>
            <td>姓名</td>
            <td>年龄</td>
            <td>邮箱</td>
            <td>操作</td>
        </tr>
        {%for author in authors%}
            <tr>
                <td>{{author.name}}</td>
                <td>{{author.age}}</td>
                <td>{{author.email}}</td>
                <td>
                    <a href="{%url 'del' author.id%}">删除</a>
                </td>
            </tr>
        {%endfor%}
    </table>
</body>
</html>

.views文件

def author_list_views(request):
    authors = Author.objects.all()
    return render(request, 'author_list.html', locals())


def del_user_views(request, uid):
    Author.objects.get(id=uid).delete()
    return HttpResponseRedirect('/author_list')

猜你喜欢

转载自www.cnblogs.com/-hjj/p/9944843.html