我想在网页的views.py内的方法中显示一些从mysql查询的值,但是在网页上看不到。在views.py中查看我的方法:
def update_delete_customer(request, id):
if request.method == 'POST':
print("TODO")
else:
form = CustomerForm()
if id is None:
print("id is None")
return HttpResponseRedirect('AddressTableMaintain/')
print("----------------")
print(id)
customerList = Customer.objects.raw("select * from customers where customer_id = %s", [int(id)])
customer = list(customerList)[0]
print(customer)
form.customerId = customer.customerId
form.first_name = customer.first_name
form.last_name = customer.last_name
print(customer.customerId)
print(customer.first_name)
print(customer.last_name)
return render(request, 'EditCustomer.html', {'form': form})
return HttpResponseRedirect('AddressTableMaintain/')
我可以在控制台中看到由打印功能生成的输出客户ID,名字,姓氏。 以下是我的EditCustomer.html的内容:
{% extends 'base.html' %}
{% block title %} Remove or Update customer {% endblock %}
{% block content %}
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
<form action="{% url 'RU_customer_post' %}" method="post">
{% csrf_token %}
<table width="50%" align="center">
<tr>
<td>Customer ID</td>
<td>
{{ form.customer_id }}
</td>
</tr>
<tr>
<td>First Name</td>
<td>
{{ form.first_name }}
</td>
</tr>
<tr>
<td>Last Name</td>
<td>
{{ form.last_name }}
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value = "OK" /></td>
</tr>
</table>
</form>
{% endblock %}
我的预期结果是该值应绑定到网页并显示,但实际效果如下:
那么,有人可以告诉我原因以及如何解决吗?
答案 0 :(得分:0)
替换:
form = CustomerForm()
if id is None:
print("id is None")
return HttpResponseRedirect('AddressTableMaintain/')
print("----------------")
print(id)
customerList = Customer.objects.raw("select * from customers where customer_id = %s", [int(id)])
customer = list(customerList)[0]
具有:
if id is None:
print("id is None")
return HttpResponseRedirect('AddressTableMaintain/')
print("----------------")
print(id)
customerList = Customer.objects.raw("select * from customers where customer_id = %s", [int(id)])
form = CustomerForm(list(customerList)[0])
可能与之相关:为什么要使用原始SQL而不是更简单的方法:
customer = Customer.objects.get(pk=id)
哪个会返回您正在寻找的一位客户?