cannot concatenate 'str' and 'list' objects

Chris Angelico rosuav at gmail.com
Sat Sep 15 11:44:14 EDT 2012


On Sun, Sep 16, 2012 at 1:23 AM, Νικόλαος Κούρας <nikos.gr33k at gmail.com> wrote:
> I swicthed back my code to:  f = open( '../' + page )
>
> and http://superhost.gr works which means that line gets parsed correctly.
>
> Only when i post data to the text area boxes iam getting the error:
>
> cannot concatenate 'str' and 'list' objects
>
> how is this possible to work before and NOT work when i enter data to the page for storing to the database?

Okay. Putting everything together, I think I can see what's going on.

You have a rewrite rule that's changing http://superhost.gr/ into a
request that, if my hand-parsing of Apache directives is working,
looks like this:
http://superhost.gr/cgi-bin/counter.py?page=index.html

That page doesn't quite work, as the browser goes looking for images
and CSS in cgi-bin instead of the root, but that's pretty much what's
going on. (I had to guess that it was index.html, as that part is
implicit in the original URL.)

You then have a request with a GET variable named 'page', and also a
POST variable named 'page', courtesy of the hidden form variable. This
would be why you're getting a list instead of a string.

Is the hidden form field completely redundant? If so, just remove it.
If you need that information for some other reason (eg you want to
record which page the user came from), then rename one or other of
them.

I would recommend a naming convention whereby you keep "internal
stuff" away from your main site. For instance, change your rewrite
rule to create a form field called _page and have counter.py look for
that. Or better still, don't use the query string for that at all, if
you can (I can't remember off-hand what a rewrite rule can set, but I
think you can carry extra information around in other ways).
Currently, your site can be broken by adding a query string to the
URL:

http://superhost.gr/?page=hahahaha

As a general rule, a web site should not get confused by the presence
of an unexpected form field. It's not good for someone to be able to
break your page like this!

It appears that part of the problem is that your getvalue() function
has three possibilities, similar to the issue I had a while ago with a
similar PHP function. Readers of CS Lewis may recognize what I'm
saying... Either she is lying, and there is no array at all; or she is
mad - a single item as a scalar instead of an array; or she is telling
the truth, and we have an array just like we need.

ChrisA



More information about the Python-list mailing list