1. 程式人生 > >查詢介面練習

查詢介面練習

查詢介面練習

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')