通过django将数据显示为模态形式

问题描述 投票:0回答:1

我对 django 非常陌生。单击详细信息按钮后,我试图以模式形式显示学生记录的详细信息。但它显示的是一个空的模态形式。下面是代码片段。如果我在这里做错了什么,请告诉我。预先感谢。

以下是观点

def student(request):
    students = Student.objects.values("standard__standard_name", "studentid","firstname","lastname","status","nationality","Gender")
    return render(request, 'student.html', { 'students':students })

def student_detail(request, studentid):
    try:
        student_object = Student.objects.filter(studentid=studentid)
        student = student_object.values("studentid","firstname","lastname","standard__standard_name","status","nationality","dob","Gender","religion","parent__parentid")
        print(student)
    except student.DoesNotExist:
        raise Http404('student not found')
    return render(request, 'student_detail.html', {'student':student})

student.html 片段

     <div class="tab-pane fade show active" id="show-students-pane" role="tabpanel" aria-labelledby="show-students" tabindex="0">
        <table class="table table-striped table-bordered table-hover">
          <th>Name</th>
          <th>Standard</th>
          <th>Gender</th>
          <th>Status</th>
          <th>Nationality</th>
          {% for student in students %}
          <div class="studentname">
            <tr>
              <td>{{ student.firstname |capfirst }} {{ student.lastname |capfirst }}</td></a>
              <td>{{ student.standard__standard_name }}</td>
              <td>{{ student.Gender }} </td>
              <td>{{ student.status }} </td>
              <td>{{ student.nationality }} </td>
              <td><button type="button" class="modalbtn btn btn-primary" data-bs-toggle="modal" data-bs-target="#studentModal" onclick="localtion.href='/student/<studentid>/detail/ student.studentid'">Details</button></td>
            </tr>
          </div>
          {% endfor %}
        </table>
        <div class="modal" id="studentModal">
          <div class="modal-dialog">
            <div class="modal-content">

              <!-- Modal Header -->
              <div class="modal-header">
                <h4 class="modal-title" id="studentModal">{{ student.firstname }}</h4>
                <button type="button" class="btn-close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
              </div>

              <!-- Modal body -->
              <div class="modal-body">

                {% include 'student_detail.html' %}
              </div>

              <!-- Modal footer -->
              <div class="modal-footer">
                <button type="button" class="btn btn-danger" data-bs-dismiss="modal">Close</button>
              </div>

            </div>
          </div>
        </div>
      </div>

student_detail.html


<table class="table table-striped table-bordered table-hover" >
  {% for s in student %}
  <tr>
      <th>Name</th>
      <td>{{ s.firstname |capfirst }} {{ s.lastname|capfirst }}</td>
  </tr>
  <tr>
      <th>FirstName</th>
      <td>{{ s.firstname }}</td>
  </tr>
  <tr>
      <th>LastName</th>
      <td>{{ s.lastname }}</td>
  </tr>
  <tr>
      <th>Standard</th>
      <td>{{ s.standard__standard_name }}</td>
  </tr>
  <tr>
      <th>DOB</th>
      <td>{{ s.dob }}</td>
  </tr>
  <tr>
      <th>Gender</th>
      <td>{{ s.Gender }}</td>
  </tr>
  <tr>
      <th>Nationality</th>
      <td>{{ s.nationality }}</td>
  </tr>
  <tr>
      <th>Religion</th>
      <td>{{ s.religion }}</td>
  </tr>
  <tr>
    <th>Parent</th>
    <td>
      {{ s.parent__parentid }}
    </td>
</tr>
  {% endfor %}
 
</table>

urls.py

    path('student/', views.student, name='student'),
    path('student/<studentid>/detail/', views.student_detail, name='student_detail'),
html css django bootstrap-modal
1个回答
0
投票

我想知道问题是什么。在学生详细信息页面中,您仅通过了一名学生! 那你为什么要循环??? 你的标签应该是这样的:

{{student.first_name}}
{{student.last_name}}

并且不需要 for 循环

我还有其他建议!

您的 Student_detail 网址:

path('student/<int:studentid>/detail/', views.student_detail, name='student_detail')

我刚刚在studentid之前添加了and int,我认为这是一个更好的做法,

另一个建议是在详细信息按钮中使用 django 标签和一个由 js 组成的 a 标签:

<a href="{% url 'student_detail' %}">Details</a>

在这些情况下,我自己更喜欢标签而不是按钮

© www.soinside.com 2019 - 2024. All rights reserved.