'str' object has no attribute 'write'

364 views
Skip to first unread message

mary

unread,
May 25, 2006, 6:19:13 AM5/25/06
to Django users
i wrote this view
def index(request):
output=open( '/var/www/xml-python/xmleditor/test.xml','w')
output.write('<?xml version="1.0" encoding="iso-8859-1"?>')
output.write('<content_topnav>')
latest_menu_list = Menu.objects.all().order_by('-id')
output = ', '.join([m.menu_text for m in latest_menu_list])
output.write(output)

return HttpResponse(output)

and i got this error
'str' object has no attribute 'write'


can anyone help please

Thanks,
Mary

Jay Parlar

unread,
May 25, 2006, 10:51:47 AM5/25/06
to django...@googlegroups.com

When you do this:

output = ', '.join([m.menu_text for m in latest_menu_list])

you rebound the name 'output' to a string, it no longer references
your file descriptor. Try this:

output_str = ', '.join([m.menu_text for m in latest_menu_list])
output.write(output_str)

John Melesky

unread,
May 25, 2006, 10:53:47 AM5/25/06
to django...@googlegroups.com, mary...@itrize.com
mary wrote:
> and i got this error
> 'str' object has no attribute 'write'
...

> output = ', '.join([m.menu_text for m in latest_menu_list])
> output.write(output)

In the first line there, you're setting "output" (which used to be a
file object) to a string, and then in the next line you're trying to
call the "write" method again (which would work if it were still a file
object, but it's not anymore).

Do you instead mean this:

output.write(', '.join([m.menu_text for m in latest_menu_list]))

or even:

menulist = ', '.join([m.menu_text for m in latest_menu_list])
output.write(menulist)

Either of those two should work.


-johnnnnnnnn

Ivan Sagalaev

unread,
May 25, 2006, 11:39:15 AM5/25/06
to django...@googlegroups.com
mary wrote:

>i wrote this view
>def index(request):
> output=open( '/var/www/xml-python/xmleditor/test.xml','w')
> output.write('<?xml version="1.0" encoding="iso-8859-1"?>')
> output.write('<content_topnav>')
> latest_menu_list = Menu.objects.all().order_by('-id')
> output = ', '.join([m.menu_text for m in latest_menu_list])
> output.write(output)
>
>
>
> return HttpResponse(output)
>
>

After fixing what others have pointed you will probably have a next
problem with empty output in a browser. You're passing your "output"
object to HttpResponse while output's position is at its end. You should
move to the beginning:

output.seek(0)
return HttpResponse(output)

Reply all
Reply to author
Forward
0 new messages