MacPython
MacPython

Reputation: 18271

Django read from db

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)

Upvotes: 1

Views: 1303

Answers (3)

dmitko
dmitko

Reputation: 2657

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

Upvotes: 1

Manoj Govindan
Manoj Govindan

Reputation: 74705

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.

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599590

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

Upvotes: 1

Related Questions