【发布时间】:2020-02-24 14:15:38
【问题描述】:
views.py
from .models import Customer as CustomerModel
class Balance_Enquiry(TemplateView):
template_name = 'balance_enuiry.html'
def get(self, request, *args, **kwargs):
if "user_id" in request.session:
try:
customer = CustomerModel.objects.get(user_id=request.session["user_id"])
args = {'form': form, 'posts': customer}
return render(request, self.template_name, args)
except Exception as e:
return HttpResponse('failed: {}'.format(e), 500)
模板
{% extends 'home.html' %}
{% load crispy_forms_tags %}
{% block title %}Balance Enquiry{% endblock %}
{% block content %}
<form method="get" >
{% csrf_token %}
<table border="5">
<tr>
<th>Amount</th>
<th>Contact</th>
</tr>
{% for get in customer %}
<tr>
<td>{{ get.amount }}</td>
<td>{{ get.contact }}</td>
</tr>
{% endfor %}
我只想在登录后获取单人记录,它显示该人的记录,但它没有在屏幕上显示任何内容,它只显示空白,甚至没有任何错误
【问题讨论】:
-
这里
args = {'form': form, 'posts': customer}将客户置于上下文中,然后在模板中遍历customer。这是在哪里定义的?为什么您将客户称为posts? -
我收到错误“客户”对象不可迭代
-
从您的代码来看,
customer是什么并不明显。你能在上下文中显示这个变量的定义吗?另外,您为什么要通过单个记录进行迭代? -
在客户中我存储客户表的对象
-
我只想获取登录意味着他的联系人_无金额等的人的单一记录
标签: django python-3.x postgresql