iT邦幫忙

0

Bootstrap Table如何搭配Django使用呢...

各位大神好
新手想請教Bootstrap Table如何搭配Django使用呢...

https://bootstrap-table.com/
html碼是照抄範例 有試著在加上datatype="json"還有data-url="/makelist"
https://examples.bootstrap-table.com/#view-source
打開list最後顯示卻是"沒有找到符合的結果"

以下是一些設定
urls.py

    path('list/', views.list, name='list'),
    path('makelist/', views.makelist, name='makelist'),

views.py

def makelist(request):
    something = Something.objects.all()
    newdata = serializers.serialize('json', something)
    data = {
        'equipments': newdata,
    }
    return HttpResponse(json.dumps(data), content_type='application/json')

def list(request):
    return render(request, 'list.html')

最後想再問一下...
為什麼我右上角toolbar的icon都出不來呢

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

    <link href="https://unpkg.com/bootstrap-table@1.19.1/dist/bootstrap-table.min.css" rel="stylesheet">
    <script src="https://unpkg.com/tableexport.jquery.plugin/tableExport.min.js"></script>
    <script src="https://unpkg.com/bootstrap-table@1.19.1/dist/bootstrap-table.min.js"></script>
    <script src="https://unpkg.com/bootstrap-table@1.19.1/dist/bootstrap-table-locale-all.min.js"></script>
    <script src="https://unpkg.com/bootstrap-table@1.19.1/dist/extensions/export/bootstrap-table-export.min.js"></script>
</head>

已經加了好多的css, js了

1 個回答

0
souda
iT邦好手 1 級 ‧ 2021-12-10 07:58:54

您可以這麼做!

PY程式碼原文

def makelist(request):
    something = Something.objects.all()
    newdata = serializers.serialize('json', something)
    data = {
        'equipments': newdata,
    }
    return HttpResponse(json.dumps(data), content_type='application/json')

PY程式碼改變

def makelist(request):
    something = Something.objects.all()
    return render(
        'XXX.html',
        {'post_list': something},
    )

HTML改變

<html>
...(省)
<script>
...(省)
</script>
<body>
  <tbody>
    {% for r in post_list %}
    <tr>
      <td>{{r.XXX}} </td>
      .....(省)
    </tr>
    {% endfor %}                            
  </tbody>
</body>
...(省)
</html>

我要發表回答

立即登入回答