'When Displaying Django Variables, 'Could not parse the remainder' error

I have a Django app that is meant to display the records of certain people. Rather than making a template for each person's records, I wanted to create one view and one template that can display the records of different people dynamically by taking the person's name from a variable passed through the url. All of the rest of my code seems to run fine, but when I render the template the variables containing the person's information I get this error:

Could not parse the remainder: '('first_name',flat=True[0]'from'modelname.objects.values_list('first_name', flat=True)[0]'

I have stored information about the people in several different models which are contained in the records variable as a list. Firstname is the variable containing the name entered into the url by the user.

Sorry if my code is ugly. Thank you for your time.

views.py

def records(response, firstname):

#firstname is a variable containing the name entered into the url by the user
#the function will check if a record model with that first name is in records
#records is a list containing all record models

foundmodel = False

for i in range(len(records)):
    firstname = firstname[0].upper() + firstname[1:] #Makes first letter uppercase
    if firstname == records[i].objects.values_list('first_name', flat=True)[0]:
        modelname = records[i]

#modelname will be passed to the template so the correct model can be referenced

        foundmodel = True
        break
    else:
        continue
 #the loop will keep iterating until a match is found or there are no more record models to try

if foundmodel == True:
    return render(response, 'base2.html', {'modelname':modelname})

 #the template will be rendered with the information from the correct model if a model with the 
 #entered name is found

if foundmodel == False:
    return HttpResponse('Person does not exist')

#if a model with the entered name is not found, this page will be rendered

relevant base2.html, which will be rendered if a model with the chosen name is found

<div class="col-8-xs">
        <img style="width:100px; height:100px;" alt="person's picture'" src="#">

        <br>
        <br>
        <br>
        <br>

        <p>Full Name:{{modelname.objects.values_list('first_name', flat=True)[0]}}&nbsp;{{modelname.objects.values_list('last_name', flat=True)[0]}}</p>

        <p>Age:{{modelname.objects.values_list('age', flat=True)[0]}}</p>

        <p>Occupation:{{modelname.objects.values_list('occupation', flat=True)[0]}}</p>
    </div>

urls.py

from django.urls import path

from app_3 import views

urlpatterns = [
    path('home', views.homepage, name='homepage'),
    path('base', views.base, name='base'),
    path('<str:firstname>/records', views.records, name='records')

]


Solution 1:[1]

Your displaying logic in the template, which belongs in the view.

Solution 1 (recommended)

Not sure why need to do all the values_list(), but maybe I'm missing something. Assuming your modelname class has the fields first_name, last_name, occupation, then all you need to do in the template is:

<p>Full Name:{{ modelname.first_name }} {{ modelname.last_name }}</p>
<p>Age:{{ modelname.age }}</p>
<p>Occupation:{{ modelname.occupation }}</p>

Solution 2
Now if there is logic that you need to do, write it in your views, and then pass it along as variables, like below (but again, I do NOT think this is what you need or want):

def records(response, firstname):

    context = {
        'first_name': modelname.objects.values_list('first_name', flat=True)[0]
        'last_name': modelname.objects.values_list('last_name', flat=True)[0]
        'age': modelname.objects.values_list('age', flat=True)[0]
        'occupation': modelname.objects.values_list('occupation', flat=True)[0] 
    }

    return render(response, 'base2.html', context)

And then access these variables in your template like this:

<p>Full Name: {{ first_name }} {{ last_name }}</p>
<p>Age: {{ age }}</p>
<p>Occupation: {{ occupation }}</p>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 raphael