如何在Django模板中显示模型功能 [英] How to display model fuction in Django template

查看:58
本文介绍了如何在Django模板中显示模型功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是从模型中获取数据以显示引导程序模态。我现在正在做的是尝试将函数数据传递到视图中,并通过视图尝试在模式中显示with。我遇到的问题是:



第47行的屏蔽标签无效: show_profile,预期为空或 endfor。您忘了注册或加载此标签吗?



我是在尝试传递数据时做错了什么,还是我的标签或其他问题出了问题?



模型

  class mentee(models.Model):
application_date_time = models.DateTimeField()
#status = models.CharField(max_length = 30)
fname = models.CharField(max_length = 30)
full_name = models.CharField(max_length = 30)
lname = models.CharField(max_length = 30)
email = models.CharField(max_length = 30)
phone = models.CharField(max_length = 30)
生物=模型.CharField(max_length = 1700)
性别=模型.CharField(max_length = 30)
目标=模型.CharField(max_length = 100)
years_of_experience = models.CharField(max_length = 20)
university = models.CharField(max_length = 30)
Graduate_year = models.IntegerField()
ma jor = models.CharField(max_length = 30)
class_standing = models.CharField(max_length = 30)
city = models.CharField(max_length = 30)
country = models.CharField(max_length = 30)
student_type = models.CharField(max_length = 30)
profile_pic = models.CharField(max_length = 30)
linkedin = models.CharField(max_length = 30)
GitHub = models.CharField(max_length = 30)
网站= models.CharField(max_length = 30)
简历= models.CharField(max_length = 100)
area_of_concentration = models.CharField(max_length = 30)
role_of_interest = models.CharField(max_length = 100)
met_in_person = models.BooleanField()
preferred_years_of_experience = models.CharField(max_length = 20)
organization_of_interest = models.CharField(max_length = 30)
area_of_expertise = models.CharField(max_length = 100)
skill_desired = ArrayField(ArrayField(model.CharField(max_length = 100)))
female_preference = models.CharField(max_length = 30)
time_preference = ArrayField(ArrayField(models.CharField(max_length = 30)))
location_preference = ArrayField(ArrayField(models.CharField(max_length = 30)))
preference_notes = models.CharField(max_length = 1700)

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

HTML



<课前= lang-html prettyprint-override>
< 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>
< h5 class = modal-title id = exampleModalLabel>模式标题< / h5>
< button type = button class = close data-dismiss = modal aria-label = Close>
< span aria-hidden = true& times;< / span>
< / button>
< / div>
< div class = modal-body>
{%show_profile%}
< / div>
< div class = modal-footer>
< button type = button class = btn btn-secondary data-dismiss = modal> Close< / button>
< / div>
< / div>
< / div>
< / div>



  def home(request) :
if request.method =='GET':
data = mentee.objects.all()
profile = mentee.showprofile(mentee.full_name)
stu = {
full_name:数据,
show_profile:配置文件

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


解决方案

更改此行

  {%show_profile%} 

  {%for show_profile%中的i} 
{{i.full_name}}
{%endfor%}

和您的方法

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

  def showprofile(self,full_name):
返回self.objects.fil ter(full_name = fullname)


What I am trying to do is to get the data from my model to show up a bootstrap modal. What I am doing right now is trying to pass the function data into the view and through the view I am trying to show the with in the modal. The issue I am encountering is:

Invalid block tag on line 47: 'show_profile', expected 'empty' or 'endfor'. Did you forget to register or load this tag?

Am I doing something wrong when I am trying to pass the data in or is something wrong with my tag or something else?

Model

class mentee(models.Model):
    application_date_time = models.DateTimeField()
    # status = models.CharField(max_length=30)
    fname = models.CharField(max_length=30)
    full_name = models.CharField(max_length=30)
    lname = models.CharField(max_length=30)
    email = models.CharField(max_length=30)
    phone = models.CharField(max_length=30)
    bio = models.CharField(max_length=1700)
    gender = models.CharField(max_length=30)
    objectives = models.CharField(max_length=100)
    years_of_experience = models.CharField(max_length=20)
    university = models.CharField(max_length=30)
    graduation_year = models.IntegerField()
    major = models.CharField(max_length=30)
    class_standing = models.CharField(max_length=30)
    city = models.CharField(max_length=30)
    country = models.CharField(max_length=30)
    student_type = models.CharField(max_length=30)
    profile_pic = models.CharField(max_length=30)
    linkedin = models.CharField(max_length=30)
    GitHub = models.CharField(max_length=30)
    website = models.CharField(max_length=30)
    resume = models.CharField(max_length=100)
    area_of_concentration = models.CharField(max_length=30)
    roles_of_interest = models.CharField(max_length=100)
    meet_in_person = models.BooleanField()
    preferred_years_of_experience = models.CharField(max_length=20)
    organization_of_interest = models.CharField(max_length=30)
    area_of_expertise = models.CharField(max_length=100)
    skills_desired = ArrayField(ArrayField(models.CharField(max_length=100)))
    gender_preference = models.CharField(max_length=30)
    time_preference = ArrayField(ArrayField(models.CharField(max_length=30)))
    location_preference = ArrayField(ArrayField(models.CharField(max_length=30)))
    preference_notes = models.CharField(max_length=1700)

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

HTML


             <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">
                    <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    </div>
                    <div class="modal-body">
                    {% show_profile %}
                    </div>
                    <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    </div>
                </div>
                </div>
            </div>

def home(request):
    if request.method == 'GET':
        data = mentee.objects.all()
        profiles = mentee.showprofile(mentee.full_name)
        stu = {
            "full_name": data,
            "show_profile": profiles

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

解决方案

change this line

{% show_profile %}

to

{% for i in show_profile %}
    {{ i.full_name }}
{% endfor %}

and your method from

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

to

def showprofile(self,full_name):
       return self.objects.filter(full_name=fullname)

这篇关于如何在Django模板中显示模型功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆