On Jul 13, 2010, at 1:56 PM, Rick wrote:

> This gives no error:
> dict()[x] = [x]
> ..but my web page is still not shown in the browser, it's just printed
> "None". Maybe the controller-file-code is correct but something should
> be added to the view file?

It's not at all clear what you're trying to do. The line above creates a dict 
with a single entry whose key is x, and whose value is a list with x as a 
member. That doesn't seem very useful.

If you want a variable number of forms to be displayed, try creating a list of 
forms, and passing the list to your view in a dict as usual. The code that 
JmiXIII gave is a good example:

def listform():
 listf=[]
 thing = [one two three]
 for x in thing:
    form=FORM(':', INPUT(_name="name"))
    listf.append(form)
return dict(listf=listf)

In your view, you'll have to loop through the list and output each form.

One problem with the code above is that you'll need to give each form a unique 
name. See "Multiple forms per page" in the manual, Chapter 7.


> 
> On Jul 13, 12:19 am, Jonathan Lundell <[email protected]> wrote:
>> On Jul 12, 2010, at 3:04 PM, Rick wrote:
>> 
>>> Thanks for your inspiring answer,
>> 
>>> After I found this page:
>>> http://code.activestate.com/recipes/440502-a-dictionary-with-multiple...
>>> ...I tried with:
>>> return dict['form'].append([x])
>>> ...but my new code doesn't work neither. I just get "TypeError: 'type'
>>> object is unsubscriptable".
>> 
>> Don't use the word 'dict'.
>> 
>> 
>> 
>>> Though I think this code is nearer the
>>> solution.
>> 
>>> On Jul 12, 10:44 pm, JmiXIII <[email protected]> wrote:
>>>> Hello ,
>> 
>>>> I've used something like this :
>>>> def listform():
>>>>   listf=[]
>>>>   thing = [one two three]
>>>>   for x in thing:
>>>>      form=FORM(':', INPUT(_name="name"))
>>>>      listf.append(form)
>>>> return dict(listf=listf)
>> 
>>>> Yet I usually use SQLFORM and add a submit button
>> 
>>>> As this is my firts answer to a coding question, do not hesitate to
>>>> tell if I'm wrong
>> 
>>>> On 12 juil, 22:26, Rick <[email protected]> wrote:
>> 
>>>>> Hi,
>> 
>>>>> How to generate multiple forms with a loop?
>> 
>>>>> In my controller file there are forms generated with this loop:
>> 
>>>>> thing=[one, two, three]
>>>>> def theFunction():
>>>>>         for thing1  in varibale1:
>>>>>                 form=FORM(':',
>>>>>                                 INPUT(_name='name')
>>>>>                         )
>>>>>                 return dict(form=form)
>> 
>>>>> ...but this code doesn't work. I just get the message "invalid view".
>>>>> I suppose the reason that I get this message is that all the forms
>>>>> have the same name in the view file. Therefor I also tried with:
>>>>>         return dict(form=[thing])
>>>>> ...but got:
>>>>> SyntaxError: keyword can't be an expression
>> 
>>>>> I've tried with this code in the view file:
>> 
>>>>>         {{extend 'layout.html'}}
>>>>>         <h2>{{=form}}</h2>
>> 
>>>>> ...and also with this:
>> 
>>>>>         {{extend 'layout.html'}}
>>>>>         <h2>
>>>>>         {{thing=[one, two, three]}}
>>>>>         {{for thing1  in varibale1:}}
>>>>>                 {{=form}}
>>>>>         </h2>
>> 
>>>>> ...but none of them worked.
>> 
>>>>> Thanks in advance for help


Reply via email to