如何使用Django中的按钮动态显示模型中的数据?

时间:2019-05-29 07:46:56

标签: python django django-models django-templates bootstrap-modal

我正在尝试从模型中动态加载数据,我遇到的问题是我无法动态获取数据,例如,这是模型:

class mentee(models.Model):
    application_date_time = models.DateTimeField()
    full_name = models.CharField(max_length=100)
    email = models.CharField(max_length=50)
    phone = models.CharField(max_length=50)
    gender = models.CharField(max_length=50)
    university = models.CharField(max_length=500)
    major = models.CharField(max_length=50)
    class_standing = models.CharField(max_length=50)
    city = models.CharField(max_length=50)
    country = models.CharField(max_length=50)
    student_type = models.CharField(max_length=50)
    profile_pic = models.CharField(max_length=1000)
    linkedin = models.CharField(max_length=1000)
    github = models.CharField(max_length=1000)
    website = models.CharField(max_length=1000)
    resume = models.CharField(max_length=1000)
    area_of_concentration = models.CharField(max_length=50)
    roles_of_interest = models.CharField(max_length=100)
    meet_in_person = models.CharField(max_length=100)
    preferred_years_of_experience = models.CharField(max_length=50)
    organization_of_interest = models.CharField(max_length=50)
    area_of_expertise = models.CharField(max_length=100)
    skills_desired = ArrayField(ArrayField(models.CharField(max_length=100)))
    gender_preference = models.CharField(max_length=50)
    time_preference = models.CharField(max_length=50)
    location_preference = models.CharField(max_length=50)

在此函数中,我尝试调用的是这个

    def showprofile(full_name):
       mentee_data = mentee.objects.filter(full_name="Katha Benterman")
       return mentee_data.values()

这应该做的是,它应该在引导程序模型中显示数据,该模型在HTML中设置如下:

        <td><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">Profile</button></td>
             <!-- Modal -->
             <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    </div>
                    <div class="modal-body">
                    {% for profile in show_profile %}
                        {{ profile.full_name }}
                    </br>
                        {{ profile.email }}
                    </br>

                        {{ profile.phone }}
                    </br>

                        {{ profile.bio }}
                    </br>
                        {{ profile.gender }}
                    </br>
                        {{ profile.objectives }}
                    </br>
                        {{ profile.years_of_experience }}
                    </br>
                        {{ profile.university }}
                    </br>
                        {{ profile.graduation_year }}
                    </br>
                        {{ profile.major }}
                    </br>
                        {{ profile.class_standing }}
                    </br>
                        {{ profile.city }}
                    </br>
                        {{ profile.country }}
                    </br>
                        {{ profile.student_type }}
                    </br>
                        {{ profile.profile_pic }}
                    </br>
                        {{ profile.linkedin }}
                    </br>
                        {{ profile.GitHub }}
                    </br>
                        {{ profile.website }}
                    </br>
                        {{ profile.resume }}
                    </br>
                        {{ profile.area_of_concentration }}
                    </br>
                        {{ profile.roles_of_interest }}
                    </br>
                        {{ profile.skills_desired }}
                    </br>
                    {% endfor %}
                    </div>
                    <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    </div>
                </div>
                </div>

问题是当我从full_name静态指定一个名称时,它将加载数据,但是当我尝试动态进行操作时,它什么也没有显示,我认为这可能是我的函数存在的问题和我的按钮。任何帮助将不胜感激!

这是视图文件:


def home(request):
    if request.method == 'GET':
        data = mentee.objects.all()
        profiles = mentee.showprofile(mentee.full_name)

        mentee_data = {
            "full_name": data,
            "email": data,
            "email": data,
            "phone" : data,
            "bio" : data,
            "gender" : data,
            "objectives" : data,
            "years_of_experience" : data,
            "university" : data,
            "graduation_year" : data,
            "major" : data,
            "class_standing" : data,
            "city" : data,
            "country" : data,
            "student_type" : data,
            "profile_pic" : data,
            "linkedin" : data,
            "GitHub" : data,
            "website" : data,
            "resume" : data,
            "area_of_concentration" : data,
            "roles_of_interest" : data,
            "skills_desired": data,
            "show_profile": profiles

        }

        return render(request, 'main/index.html', mentee_data)

0 个答案:

没有答案