Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #47652 > unrolled thread

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

Started byΝικόλαος Κούρας <nikos.gr33k@gmail.com>
First post2013-06-11 03:38 -0700
Last post2013-06-12 06:16 +0000
Articles 9 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  Receing a form variable as a list instead of as a string Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-11 03:38 -0700
    Re: Receing a form variable as a list instead of as a string Andreas Perstinger <andipersti@gmail.com> - 2013-06-11 14:01 +0200
    Re: Receing a form variable as a list instead of as a string Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-06-11 13:51 +0200
      Re: Receing a form variable as a list instead of as a string Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-11 12:42 -0700
        Re: Receing a form variable as a list instead of as a string Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-11 13:16 -0700
          Re: Receing a form variable as a list instead of as a string alex23 <wuwei23@gmail.com> - 2013-06-11 18:06 -0700
        Re: Receing a form variable as a list instead of as a string alex23 <wuwei23@gmail.com> - 2013-06-11 16:47 -0700
      Re: Receing a form variable as a list instead of as a string nagia.retsina@gmail.com - 2013-06-11 18:29 -0700
        Re: Receing a form variable as a list instead of as a string Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-12 06:16 +0000

#47652 — Receing a form variable as a list instead of as a string

FromΝικόλαος Κούρας <nikos.gr33k@gmail.com>
Date2013-06-11 03:38 -0700
SubjectReceing a form variable as a list instead of as a string
Message-ID<66c9a650-e7a0-46a4-92da-b08d1af3bd46@googlegroups.com>
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?

[toc] | [next] | [standalone]


#47656

FromAndreas Perstinger <andipersti@gmail.com>
Date2013-06-11 14:01 +0200
Message-ID<mailman.3017.1370952113.3114.python-list@python.org>
In reply to#47652
On 11.06.2013 12:38, Νικόλαος Κούρας wrote:
> 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

[toc] | [prev] | [next] | [standalone]


#47658

FromUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Date2013-06-11 13:51 +0200
Message-ID<8ifj8a-53i.ln1@satorlaser.homedns.org>
In reply to#47652
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

[toc] | [prev] | [next] | [standalone]


#47695

FromΝικόλαος Κούρας <nikos.gr33k@gmail.com>
Date2013-06-11 12:42 -0700
Message-ID<1a7c9670-db0c-4b94-a7f7-1c3e3944ec81@googlegroups.com>
In reply to#47658
Τη Τρίτη, 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

[toc] | [prev] | [next] | [standalone]


#47700

FromΝικόλαος Κούρας <nikos.gr33k@gmail.com>
Date2013-06-11 13:16 -0700
Message-ID<e97e6205-998f-4449-9472-ec30ff65e729@googlegroups.com>
In reply to#47695
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?

[toc] | [prev] | [next] | [standalone]


#47719

Fromalex23 <wuwei23@gmail.com>
Date2013-06-11 18:06 -0700
Message-ID<ddafc6d8-5942-4b42-992c-f59971298b41@qn4g2000pbc.googlegroups.com>
In reply to#47700
On Jun 12, 6:16 am, Νικόλαος Κούρας <nikos.gr...@gmail.com> wrote:
> 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.

[toc] | [prev] | [next] | [standalone]


#47711

Fromalex23 <wuwei23@gmail.com>
Date2013-06-11 16:47 -0700
Message-ID<108dd480-3f8c-4b37-8ded-e00581539996@mq5g2000pbb.googlegroups.com>
In reply to#47695
On Jun 12, 5:42 am, Νικόλαος Κούρας <nikos.gr...@gmail.com> wrote:
> 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_.

[toc] | [prev] | [next] | [standalone]


#47722

Fromnagia.retsina@gmail.com
Date2013-06-11 18:29 -0700
Message-ID<34da03fb-7718-46dc-85b9-655b49be4750@googlegroups.com>
In reply to#47658
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.

[toc] | [prev] | [next] | [standalone]


#47741

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-06-12 06:16 +0000
Message-ID<51b81230$0$29998$c3e8da3$5496439d@news.astraweb.com>
In reply to#47722
On Tue, 11 Jun 2013 18:29:05 -0700, nagia.retsina wrote:

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

[quote]
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".



-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web