[web2py] Creating a table or a an entry in a DB from index()
Hello, I'm new to web2py and to web frameworks in general and I am trying to learn the MVC model and web2py by coding. What I'm trying to do is to have a user create tables and entries in a DB that I have defined in dv.py: > *TestDB = DAL("sqlite://storage.sqlite")* Now in index() I'm trying to do as follows: > * > ** def index(): > * * form = FORM(INPUT(_name='name', requires=IS_NOT_EMPTY()),* * INPUT(_type='submit'))* * if form.process().accepted:* * TestDB().define_table(form.vars.name, Field('testField', unique=True))* * return dict(form=form)* * **return dict(form=form)* This works (i.e. no errors) but I cannot see the created entry in appadmin. >From an answer I got in StackOverflow it's because what I do in index() is not persistent. I'd like to know what is the proper way of achieving this. Thank you. Regards, Madu.
[web2py] Re: Creating a table or a an entry in a DB from index()
Thank you Alan. But the problem is that the data entered from, say index() is not available at db.py. Am I right? If a user enters entries to table "table_names", will db.py see them? Are the data persistent? Also my understanding from reading the online book is that the model files are run once when starting the application. Could you tell me how I can make the for-loop run after each user enters a entry in the table? Thank you. On Friday, April 27, 2012 11:29:30 AM UTC+9, Alan Etkin wrote: > > I think that you could use a common table for storing user input and then > have a loop in a model file for creating each new table from the stored db > definition records > > Example: > > db.define_table("table_names", Field("name")) > > for row in db(db.table_names).select(): > db.define_table(row.name, Field("my_field")) > > > > It could be possible also to use one-to-many relations for adding table > fields to each new table with form input. > > On Thursday, April 26, 2012 11:06:47 PM UTC-3, Madu wrote: >> >> >> Hello, >> >> I'm new to web2py and to web frameworks in general and I am trying to >> learn the MVC model and web2py by coding. >> What I'm trying to do is to have a user create tables and entries in a DB >> that I have defined in dv.py: >> >>> *TestDB = DAL("sqlite://storage.sqlite")* >> >> >> Now in index() I'm trying to do as follows: >> >>> * >>> ** def index(): >>> * >> >> * form = FORM(INPUT(_name='name', requires=IS_NOT_EMPTY()),* >> >> * INPUT(_type='submit'))* >> >> * if form.process().accepted:* >> >> * TestDB().define_table(form.vars.name, Field('testField', unique=True))* >> >> * return dict(form=form)* >> >> * **return dict(form=form)* >> >> >> This works (i.e. no errors) but I cannot see the created entry in >> appadmin. From an answer I got in StackOverflow it's because what I do in >> index() is not persistent. >> I'd like to know what is the proper way of achieving this. >> >> >> Thank you. >> >> >> Regards, >> Madu. >> >
[web2py] Re: Creating a table or a an entry in a DB from index()
Thank you everybody. I defined my tables in db.py but made them able to be populated from index(). It worked. I'm not sure why tables entries inserted from index() is persistent but not tables defined from index(). I'm reading through the DAL again to see what I missed. Thank you very much all. Regards, Madu. On Friday, April 27, 2012 11:06:47 AM UTC+9, Madu wrote: > > > Hello, > > I'm new to web2py and to web frameworks in general and I am trying to > learn the MVC model and web2py by coding. > What I'm trying to do is to have a user create tables and entries in a DB > that I have defined in dv.py: > >> *TestDB = DAL("sqlite://storage.sqlite")* > > > Now in index() I'm trying to do as follows: > >> * >> ** def index(): >> * > > * form = FORM(INPUT(_name='name', requires=IS_NOT_EMPTY()),* > > * INPUT(_type='submit'))* > > * if form.process().accepted:* > > * TestDB().define_table(form.vars.name, Field('testField', unique=True))* > > * return dict(form=form)* > > * **return dict(form=form)* > > > This works (i.e. no errors) but I cannot see the created entry in > appadmin. From an answer I got in StackOverflow it's because what I do in > index() is not persistent. > I'd like to know what is the proper way of achieving this. > > > Thank you. > > > Regards, > Madu. >
[web2py] Very basic question about href and redirect
Sorry for this basic question but I don't why the hyperlink is not working: New DB Insert What I want is to redirect to this URL when the link is clicked, but when I load the page it automatically redirects. I have experienced this with other HTML tags too. Can somebody please explain why the Python code is executed before the user clicks the link? And the correct web2py way of doing it. Thank you.
[web2py] Re: Very basic question about href and redirect
Thanks pbreit! That was it. So whatever Python code inside {{}} will be executed, irrespective of the link is clicked or not. On Friday, May 4, 2012 12:30:46 PM UTC+9, pbreit wrote: > > I think all you want is this: > New DB Insert >
[web2py] Re: Very basic question about href and redirect
Thank you very much Anthony, as always for your very detailed explanations. Made it really clear for me. So the web2py goes through the .html lines and parses them and when it encounters a code within {{}} it executes it. In the case of just {{=URL('func')}} is converts that to a text with an address of that module. On Friday, May 4, 2012 1:11:37 PM UTC+9, Anthony wrote: > > So whatever Python code inside {{}} will be executed, irrespective of the >> link is clicked or not. >> > > Note, the Python code is not sent to the browser. When a web2py URL is > requested, before returning a response to the browser, web2py executes the > template on the server. At that time, all the Python code in the template > is executed with the purpose of generating the HTML to send to the browser. > The purpose of the Python code in the template should be to generate the > HTML for the page. If you happen to call redirect() within the template, > you'll get an actual redirect right then -- the rest of the HTML will not > be generated, and the request will be aborted. > > Anthony >
[web2py] Passing a SELECT
Hello, I'm trying to pass a SELECT as follows: def showToolEntries(): if request.args(0) in ToolsDB.tables: return dict(entries=SELECT("Select from list", operationalToolsDB(). select(ToolsDB[request.args(0)].serial_number))) I have a DB called ToolsDB and I have several tables in that. Each table has an field called 'serial_number'. I want to pass a SELECT to the html with the tools in the selected table. > For example, if I have two rows in my table, say, tool_1 and tool_2, the > resulting drop down will have only one entry which says * > TABLE1.serial_numbertool_1tool_2*. Both tools are in the same row. I > can't understand why the two tools are shown in one entry in the drop down > select. So a SELECT drop down is actually passed but the entries are not > passed correctly. However when I do: def showToolEntries(): if request.args(0) inToolsDB.tables: return dict(entries=,ToolsDB().select(operationalToolsDB[request. args(0)].serial_number)) I can properly see the tool entries correctly printed out as: > TABLE1.serial_number > tool_1 > tool_2 Could somebody please help me with this. Thank you. Cheers.
[web2py] Re: Passing a SELECT
Thank you very much Anthony for your detailed explanation. I didn't realize the db select returns a Rows object. Your solution worked! Thank you. On Sunday, May 20, 2012 11:03:33 PM UTC+9, Anthony wrote: > > I'm trying to pass a SELECT as follows: >> def showToolEntries(): >> if request.args(0) in ToolsDB.tables: >> return dict(entries=SELECT("Select from list", operationalToolsDB >> ().select(ToolsDB[request.args(0)].serial_number))) >> > > SELECT() takes either a list or a set of positional arguments. Since you > passed "Select from list" as the first argument, it takes the result of > your db select (which is a Rows object) as a positional argument and puts > it all in a single element. The Rows object is serialized into an > HTML table, which apparently the browser compresses to a single row. > Instead, try something like this: > > SELECT("Select from list", > *[r.serial_number for r in ToolsDB().select(ToolsDB[request.args(0)]. > serial_number)]) > > Anthony >
[web2py] Best way to save a value between methods in default.py
Hi, I thought this could be trivial but so far I couldn't figure out how to. All I need to do is have a global variable in default.py. When I call one method it will store a value in this variable, and later another method will use it. What I have is something like this: --- default.py- value = 0 def saveValue(): global value value = 1 def showValuel(): return dict(entries=value) Then I thought the default.py module maybe going out of scope when the method returns, so I put 'value' in db.py, since variables defined in db.py are available globally. But I still get the value that I define in db.py, not the modified value in saveValue(). Could you please tell me if there is a recommended way in web2py to have global values? Thank you.