Zubair Amjad
Zubair Amjad

Reputation: 761

How to display model fuction in Django template

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)

Upvotes: 0

Views: 736

Answers (2)

Exprator
Exprator

Reputation: 27503

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)

Upvotes: 1

The Weckness
The Weckness

Reputation: 111

{% show_profile %} would call a template tag that needs to be registered, hence the error that you are seeing. You want to display {{ show_profile.full_name }}.

Further, your model method should be changed:

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

Assuming you don't just want to put the very specific name out there that is in your current filter.

Upvotes: 1

Related Questions