如何在Django模板中显示模型功能

时间:2019-05-23 09:51:03

标签: python django django-models django-templates

我想做的是从模型中获取数据以显示引导程序模态。我现在正在做的是尝试将函数数据传递到视图中,并通过视图尝试在模式中显示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)
    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)

2 个答案:

答案 0 :(得分:1)

更改此行

var color_prices = new Array();
color_prices["Orange"]=1;
color_prices["Blue"]=2;
color_prices["Green"]=3;

function getColorDetails()
{
    //here we rename the variable and convert from integer to object
    var colorDetails = {
                         price: 0,
                         name: undefined
                        };

    var theForm = document.forms["order-form"];

    var selectedColor = theForm.elements["COLOR"];

    for(var i = 0; i < selectedColor.length; i++)
    {
        if(selectedColor[i].checked)
        {
            //here we store the color price inside colorDetails
            colorDetails.price = color_prices[selectedColor[i].value];
            //and we add this new line where we save the name of the color
            colorDetails.name = selectedColor[i].value;
            break;
        }
    }
    return colorDetails;
}

var colorDetails = getColorDetails();

document.getElementById('colorPrice').innerHTML = colorDetails.name + ": " + colorDetails.price.toFixed(2);

{% show_profile %}

和您的方法

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

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

答案 1 :(得分:1)

{%show_profile%}将调用需要注册的模板标签,因此会出现错误。您要显示{{show_profile.full_name}}。

此外,您应该更改模型方法:

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

假设您不只是想在当前过滤器中放一个非常具体的名称。