【问题标题】:When Displaying Django Variables, 'Could not parse the remainder' error显示 Django 变量时,\'Could not parse the remainder\' 错误
【发布时间】:2023-01-18 05:00:28
【问题描述】:

我有一个 Django 应用程序,用于显示某些人的记录。我不想为每个人的记录制作一个模板,而是想创建一个视图和一个模板,可以动态显示不同人的记录。当我呈现包含此人信息的变量的模板时,出现此错误:

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

我已经将有关人员的信息存储在几个不同的模型中,这些模型作为列表包含在 records 变量中。

视图.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

相关的 base2.html,如果找到具有所选名称的模型,将呈现该文件

<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>

网址.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')

]

【问题讨论】:

  • 您还应该添加您的models.py,以及错误的完整回溯。最好用大写字母命名模型,ModelName,而不是modelname

标签: django


【解决方案1】:

您在模板中的显示逻辑,属于视图。

方案一(推荐)

不确定为什么需要做所有的 values_list(),但也许我遗漏了一些东西。假设你的 modelname 类有 first_name、last_name、occupation 字段,那么你需要在模板中做的就是:

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

方案二
现在如果有你需要做的逻辑,将其写在您的视图中,然后将其作为变量传递,如下所示(但同样,我不认为这是你需要或想要的):

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)

然后像这样访问模板中的这些变量:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 2020-08-05
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    相关资源
    最近更新 更多