如何在Django函数中设置标志以及如何在HTML中使用

时间:2019-05-28 06:47:00

标签: django django-templates

我是Django的新手。如何在函数视图和HTML中使用标志。我正在尝试做标记处于活动状态(标志== 1)时,它将呈现home / index.html。否则将呈现login.html。怎么做。

views.py

def home_view(request):
    if 'username' in request.session:
        test_objs = Test.objects.all().values()
        return render(request, 'home/index.html', {'test_objs ': test_objs })
    return redirect('/login/')

home.html

<h1 class="text-center mb-5">Test</h1>
            <table class="table">
    <thead>
      <tr>
        <th scope="col">#</th>
        <th scope="col">ID</th>
        <th scope="col">Name</th>
        <th scope="col">Phone Number</th>
        <th scope="col">Email address</th>
        <th scope="col">Address</th>
        <th scope="col" colspan="2">Actions</th>
        <th scope="col"><i style="color:green" class="fas fa-plus" onclick="test()"></i></th>
      </tr>
    </thead>
    <tbody>
    {% for test_obj in test_objs %}
      <tr>
        <th scope="row">{{ test_obj.id }}</th>
        <td>{{ test_obj.id }}</td>
        <td>{{ test_obj.name }}</td>
        <td>{{ test_obj.phone_number }}</td>
        <td>{{ test_obj.email }}</td>
          <td>{{ test_obj.address }}</td>
          <td><a href="{% url 'native:test_edit' test_obj .id %}"><i class="fas fa-edit" style="color:blue"></i></a></td>
          <td><a href="{% url 'native:test_delete' test_obj .id %}"><i class="fas fa-trash-alt" style="color:red"></i></a></td>
      </tr>
    {% endfor %}
    </tbody>
  </table>

0 个答案:

没有答案