Receing a form variable as a list instead of as a string

  • Thread starter Íéêüëáïò Êïýñáò
  • Start date
Í

Íéêüëáïò Êïýñáò

page = form.getvalue('page')

if form.getvalue('show') == 'log' or page:
# it is a python script
page = page.replace( '/home/nikos/public_html/cgi-bin/', '' )
elif os.path.exists( page ):
# it is an html template
page = page.replace( '/home/nikos/public_html/', '' )


============================
[Tue Jun 11 13:35:30 2013] [error] [client 79.103.41.173] File "/home/nikos/public_html/cgi-bin/metrites.py", line 28, in <module>, referer: http://superhost.gr/
[Tue Jun 11 13:35:30 2013] [error] [client 79.103.41.173] page = page.replace( '/home/nikos/public_html/', '' ), referer: http://superhost.gr/
[Tue Jun 11 13:35:30 2013] [error] [client 79.103.41.173] AttributeError: 'list' object has no attribute 'replace', referer: http://superhost.gr
=========================

but page is a form variable coming from a previous sumbitted form
why the error says 'page' is a list?

How to receive that form variable as a string?
 
U

Ulrich Eckhardt

Am 11.06.2013 12:38, schrieb Îικόλαος ΚοÏÏας:
File "/home/nikos/public_html/cgi-bin/metrites.py", line 28, in <module>, referer: http://xxxredactedxxx/
page = page.replace( '/home/nikos/public_html/', '' ), referer: http://xxxredactedxxx/
AttributeError: 'list' object has no attribute 'replace', referer: http://xxxredactedxxx

but page is a form variable coming from a previous sumbitted form
why the error says 'page' is a list?

Maybe because it is a list? Try e.g. "print(type(page))" or
"print(page)" to gain some insight.

How to receive that form variable as a string?

For that, you'd have to adjust the code that you received it from. If
that's not possible, convert it to a string yourself. But didn't you
want a "form variable"?


Uli
 
A

Andreas Perstinger

but page is a form variable coming from a previous sumbitted form
why the error says 'page' is a list?

RTFM:
"If the submitted form data contains more than one field with the same
name, the object retrieved by form[key] is not a FieldStorage or
MiniFieldStorage instance but a list of such instances. Similarly, in
this situation, form.getvalue(key) would return a list of strings."
http://docs.python.org/3.3/library/cgi.html#using-the-cgi-module

Bye, Andreas
 
Í

Íéêüëáïò Êïýñáò

Ôç Ôñßôç, 11 Éïõíßïõ 2013 2:51:04 ì.ì. UTC+3, ï ÷ñÞóôçò Ulrich Eckhardt Ýãñáøå:
For that, you'd have to adjust the code that you received it from. If
that's not possible, convert it to a string yourself. But didn't you
want a "form variable"?

i manages to work around it by using this:
Indeed as Andreas said i was overusing the form variable 'page'

file = form.getvalue('file') # this comes from .htaccess
page = form.getvalue('page') # this comes form metrites.py or index.html

if not page and os.path.exists( file ):
# it is an html template
page = file.replace( '/home/nikos/public_html/', '' )
elif page or form.getvalue('show'):
# it is a python script
page = page
else:
#when everything else fails default
page = page
 
Í

Íéêüëáïò Êïýñáò

But if i write it as:

if not page and os.path.exists( file ):
# it is an html template
page = file.replace( '/home/nikos/public_html/', '' )
elif page or form.getvalue('show') == 'log':
# it is a python script
page = page
elif page or form.getvalue('show') == 'stats':
page = file.replace( '/home/nikos/public_html/', '' )
==================

I get the error that i cannot use 'replac'e to a 'list'.
How can i avoid that?
 
A

alex23

Indeed as Andreas said i was overusing the form variable 'page'

No. You're simply _not_ thinking about what you're doing.
elif page or form.getvalue('show'):
        # it is a python script
        page = page
else:
        #when everything else fails default
        page = page

What do you think "page = page" is doing? Hint: you could replace
those lines with 'pass' and your code would work _exactly the same_.
 
A

alex23

I get the error that i cannot use 'replac'e to a 'list'.
How can i avoid that?

BY NOT USING STRING METHODS ON A LIST.

Ulrich even *told* you what to do: "convert it to a string yourself"

Do you do that in your code? No. Instead, you assign a value _to the
same label_ and expect it to do magic.

For someone who is adamant he's not trolling, you're sure doing your
damnedest to live up to your self-applied troll name.
 
N

nagia.retsina

if page or form.getvalue('show') == 'log':
# it is a python script
page = page.replace( '/home/nikos/public_html/cgi-bin', '' )
elif page or form.getvalue('show') == 'stats':
page = page.replace( '/home/nikos/public_html/cgi-bin', '' )


in the first if option page works a string and replace happens , while in the second it behaves liek a list and the error i mentioned is being produced.
 
S

Steven D'Aprano

if page or form.getvalue('show') == 'log':
# it is a python script
page = page.replace( '/home/nikos/public_html/cgi-bin', '' )
elif page or form.getvalue('show') == 'stats':
page = page.replace( '/home/nikos/public_html/cgi-bin', '' )


in the first if option page works a string and replace happens , while
in the second it behaves liek a list and the error i mentioned is being
produced.


It doesn't *behave* like a list. It **IS** a list. I know that people
have already pointed you to the documentation, where getvalue is
documented to return either a string or a list, depending on how many
values there are in the form.

So, yet again, here is the documentation:

http://docs.python.org/3/library/cgi.html


READ IT.

Here is an extract:

In the previous section, you learned to write following code anytime you
expected a user to post more than one value under one name:

item = form.getvalue("item")
if isinstance(item, list):
# The user is requesting more than one item.
else:
# The user is requesting only one item.
[end quote]


Read the rest of the docs. Don't ask any more questions until you have
read them. Then, if anything is still unclear, you can point to a section
of the docs and say "I don't understand this".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top