【问题标题】:How to get the selected row's record from a table in django?如何从 django 的表中获取选定行的记录?
【发布时间】:2021-09-09 15:57:31
【问题描述】:

我是 django 新手,所以如果这是一个菜鸟问题,我很抱歉。

我在 models.py 中创建了一个模型 "Students" 和一个用于添加新学生的视图 "Add_Student",以及另一个视图 "Display_Students" 用于显示所有学生。我有一张桌子,其中所有学生都完美地显示了特定的列。但在表格中,我又添加了一列 "Action",并且该列中有一个眼睛图标。现在,如果有人点击那个眼睛图标,那么完整的信息应该显示在一个新的表单/页面中。在新表单/页面中显示信息不是问题,但我不知道它将如何工作。同样,会有一个编辑和删除图标。

所以这里,我的问题来了,当我点击一个眼睛图标时,django 将如何从表中获取所需的行 id?

from django.db import models

# Create your models here.

class students(models.Model):
    firstname = models.CharField(max_length=50)
    lastname = models.CharField(max_length=50)
    fathername = models.CharField(max_length=50)
    city = models.CharField(max_length=50)
    country = models.CharField(max_length=50)
    state = models.CharField(max_length=50)
    zip = models.IntegerField(blank=True)
    address = models.CharField(max_length=100)
    contact =models.CharField(max_length=20)
    photo = models.FileField()

Views.py

from django.shortcuts import render, redirect
from home.models import students

# Create your views here.

def index(request):
    return render(request, "index.html")

def Add_Student(request):
    if request.method == 'POST':
        firstname = request.POST.get('firstname')
        lastname = request.POST['lastname']
        fathername = request.POST['fathername']
        city = request.POST['city']
        country = request.POST['country']
        state = request.POST['state']
        zipcode = request.POST['zip']
        address = request.POST['address']
        contact = request.POST['contact']
        photo = request.POST['photo']

        Add_Student = students.objects.create(firstname=firstname, lastname=lastname, fathername=fathername, city=city,country=country, state=state, zip=zipcode, address=address, contact=contact, photo=photo)
        Add_Student.save()
        return redirect('Add_Student')
    else:
        return render(request, 'student/Add_Student.html')

def Display_Student(request):
    Display_Student = students.objects.all()
    return render(request, 'student/display_students.html',{'Display_Student':Display_Student})

display_student.html

在此页面中,我们可以看到最后(操作)列中的图标。我想要当有人点击第一行的 eye-con 时,它应该从所选第一行的表中获取所有记录。在另一个页面中,更多信息将以卡片的形式显示在student_profile.html页面

<table id="example1" class="table table-bordered table-striped">
            <thead>
                <tr>
                    <th>Student Name</th>                   
                    <th>Father Name</th>                               
                    <th>Contact</th>                             
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
            
                {% for student in Display_Student %}
                <tr>
                    <td>{{student.firstname}} {{student.lastname}}</td>
                    <td>{{student.fathername}}</td>
                    <td>{{student.contact}}</td>                
                    <td>                    
                      <a href="student_profile">
                       <i class="far fa-eye"></i>
                                  
                    </a>
                    <a href="#">
                       <i class="far fa-edit"></i>
                               
                    </a>
                    <a href="#">
                       <i class="far fa-trash-alt"></i>
                                   
                    </a>
                      </td>
                </td>
                </tr>
                {% endfor %}
            
            </tbody>
           
          </table>

student_profile.html

当有人点击该眼睛图标时,完整的信息应显示在此页面中。

<div class="card card-primary card-outline">
              <div class="card-body box-profile">
                <div class="text-center">
                  <img class="profile-user-img img-fluid img-circle"
                       src="{% static 'dist/img/user4-128x128.jpg' %}"
                       alt="User profile picture">
                </div>

                <h3 class="profile-username text-center">{{student.firstname}}</h3>

                <p class="text-muted text-center">Software Engineer</p>

                <ul class="list-group list-group-unbordered mb-3">
                  <li class="list-group-item">
                    <b>Father Name</b> <a class="float-right">1,322</a>
                  </li>
                  <li class="list-group-item">
                    <b>Contact</b> <a class="float-right">543</a>
                  </li>
                  <li class="list-group-item">
                    <b>City</b> <a class="float-right">13,287</a>
                  </li>
                </ul>

                <a href="#" class="btn btn-primary btn-block"><b>Follow</b></a>
              </div>

如果有人帮助我,我们将不胜感激。请我是新手,我必须这样做,这是我最后的希望。谢谢

【问题讨论】:

    标签: python django django-rest-framework django-views django-templates


    【解决方案1】:

    display_student.html

    更新

    <a href="student_edit">
       <i class="far fa-eye"></i>
    </a>
    

    <a href="{% url 'student_edit' pk=student.pk %}">
       <i class="far fa-eye"></i>
    </a>
    

    同时将student_profile 的网址更新为

    'student/<int:pk>/edit', student_edit_view, name="student_edit"
    

    然后你可以将视图中的pk检索为

    def student_edit_view(request, pk):
        #your_code
    

    注意:pk 表示primary key

    【讨论】:

    • 您好,谢谢您的回答。我尝试了与上述相同的方法,在 Student_Profile 视图中,我检索了查询中的所有列。不,是时候在上面提到的 html 页面“student_profile.html”中显示它了,那么如何做到这一点.. 谢谢
    • 请用您创建的Student_Profile 视图更新您的问题,然后我会告诉您您需要做的确切事情。
    • 请回答这个问题。 stackoverflow.com/questions/68179323/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-26
    相关资源
    最近更新 更多