首页 > 解决方案 > CRUD 在同一页 DJANGO

问题描述

我正在尝试使用 django 仅使用一个 html 页面进行 CRUD,但我已经遇到了第一个问题,当使用导航选项卡时,当我单击“创建/编辑”选项卡时,我无法加载表单,也许它有与网址有关,但我不知道。

索引.html

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

</head>
<body>

<div class="container">
  <h2>Ordens de Serviço</h2>
  <ul class="nav nav-tabs">
    <li class="active"><a data-toggle="tab" href="#list">Ordens de Serviço</a></li>
    <li><a data-toggle="tab" href="#create-update">Criar/Editar</a></li>
    <li><a data-toggle="tab" href="#detail">Ver</a></li>
  </ul>

  <div class="tab-content">
    <div id="list" class="tab-pane fade in active">
      <h3>Ordens de Serviço</h3>
        <table class="table">
           <thead>
           <tr>
               <th>Nome</th>
               <th>TAG</th>
               <th>nivel</th>

           </tr>
           </thead>
            <tbody>
            {%for equipamento in equipamentos %}
            <tr>
                <td>{{equipamento.nome}}</td>
                <td>{{equipamento.tag}}</td>
                <td>{{equipamento.nivel}}</td>
                <td><a><button class="btn btn-primary">Editar</button></a></td>
            </tr>
            {%endfor%}
            </tbody>
        </table>
    </div>

    <div id="create-update" class="tab-pane fade">
      <h3>Criar/Editar</h3>
      <form method="post">
      {% csrf_token %}
      {{form}}
      </form>

    </div>
    <div id="detail" class="tab-pane fade">
      <h3>Menu 2</h3>
      <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam.</p>
    </div>
  </div>
</div>
<script>

</script>
</body>
</html>


视图.py:

class ArvoreListView(ListView):
    queryset = Arvore.objects.all()
    template_name = 'example/index.html'
    context_object_name = 'equipamentos'


class ArvoreCreateView(CreateView):
    form_class = ArvoreModelForm
    model = Arvore
    template_name = 'example/create.html'


网址.py

urlpatterns = [
    path('', ArvoreListView.as_view(), name='list'),
    path('create/', ArvoreCreateView.as_view(), name='create'),
]


列表显示

当我单击第二个选项卡时,表单不会呈现 创建视图

标签: djangocrud

解决方案


推荐阅读