tags:

views:

34

answers:

3

If I "read out" from a db I just saved files to I get:

Your Name is: (u'Mike',)

Your Birthday is on: (datetime.datetime(2009, 12, 5, 0, 0),)

Instead of

Your Name is Mike

Your Birthday is on the 12/05/2009

How can I achieve this?

Thanks

@ Daniel:

The same is my example you answered just before:

this is how it is saved:

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

and this how it is displayed:

    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)
A: 

How are you 'reading out' from the database? If you are connecting to the database using a library (say MySQLdb), executing a query and printing the output (say, using fetch_row()), then the result you are getting is natural.

The result will be a tuple and you'll have to extract the field you need. This can be done using an appropriate index. Say:

result = fetch_row()
print "Your Name is: %s" % result[0]

Update

(After seeing the updated question): @Daniel's answer should help you out.

Manoj Govindan
Interesting. But see my code above for how I did it. Thanks!
MacPython
+1  A: 

But that's not read from the database, it's taken straight from the form submission. Instead, use the objects you've just saved. You'll need to do a bit of extra formatting on the birthday though:

% (contact.name, number.phonenumber, contact.address, 
   number.get_phone_type_display(), 
   contact.birth_day.strftime('%d/%m/%Y'))
Daniel Roseman
But also see dmitko's answer, he has a good point.
Daniel Roseman
Mastertime! Again. Fantastic works! Thanks. How does Django know which contact to display though? Why dont I have to provide an id for Django to know which data record Django has to retrieve?
MacPython
You're not retrieving it here, because you're just using the same objects you just created. If you were in a separate view, you would need to retrieve it in the normal way with the ORM functions.
Daniel Roseman
+1  A: 
name = form.cleaned_data['name'],
birth_day = form.cleaned_data['birth_day'],
address = form.cleaned_data['address'],

I think you have tuple because of commas (,) at the end of the lines! Remove them and try again :)

dmitko
Thanks!. Actually I had two lines where there where no commas and after adding commas I had (u'') on every line. So you are right ! Why is like that? What does the comma do? Very interesting anyway! Thanks
MacPython
Putting commas means that you're creating a tuple - read this about tuples http://docs.python.org/tutorial/datastructures.html#tuples-and-sequences
dmitko