如果我从数据库中“读出”我刚保存的文件,我会得到:
你的名字是:(U‘’Mike‘,)
你的生日在:(datetime.datetime(2009,12,5,0,0),)
而不是
你的名字是迈克
你的生日是2009年5月12日
我如何才能做到这一点?
谢谢
@ Daniel:
这就是你刚才回答的我的例子:
下面是它的保存方式:
def main(request):
if request.method == 'POST':
form = UserForm(request.POST)
if form.is_valid():
name = form.cleaned_data['name'],
birth_day = form.cleaned_data['birth_day'],
address = form.cleaned_data['address'],
# contact1= form.cleaned_data['id']
phone_type = form.cleaned_data['phone_type']
phonenumber = form.cleaned_data['phonenumber']
contact = Contact(
name = form.cleaned_data['name'],
birth_day = form.cleaned_data['birth_day'],
address = form.cleaned_data['address'],
)
contact.save()
number = PhoneNumber(
# contact1 = form.cleaned_data ['id']
contact = contact,
phone_type = form.cleaned_data['phone_type'],
phonenumber = form.cleaned_data['phonenumber'],
)
number.save()
它是这样显示的:
output = '''
<html>
<head>
<title> Your telephone number </title>
</head>
<body>
<h1> Your telephone number</h1>
<p>Your Name is: %s </p>
<p>Your Telephone number is : %s </p>
<p>Your Address is: %s </p>
<p>This is your %s number </p>
<p>Your Birthday is on: %s </p>
</body>
</html>''' %( name, phonenumber, address, phone_type, birth_day)
return HttpResponse(output)
发布于 2010-09-06 09:18:30
但这不是从数据库中读取的,而是直接从表单提交中获取的。取而代之的是,使用刚刚保存的对象。不过,您需要在生日时执行一些额外的格式化操作:
% (contact.name, number.phonenumber, contact.address,
number.get_phone_type_display(),
contact.birth_day.strftime('%d/%m/%Y'))
发布于 2010-09-06 09:28:17
name = form.cleaned_data['name'],
birth_day = form.cleaned_data['birth_day'],
address = form.cleaned_data['address'],
我认为您有元组,因为在行尾有逗号(,)!删除它们,然后重试:)
发布于 2010-09-06 08:57:30
你是如何从数据库中“读出”的?如果您使用库(比方说MySQLdb
)连接到数据库,执行查询并打印输出(比方说使用fetch_row()
),那么得到的结果是很自然的。
结果将是一个元组,您必须提取所需的字段。这可以使用适当的索引来完成。可以这样说:
result = fetch_row()
print "Your Name is: %s" % result[0]
更新
(在看到更新的问题后):@Daniel的answer应该能帮到你。
https://stackoverflow.com/questions/3650128
复制相似问题