Posts

Web2py – Make your app the default web application.

There are multiple ways of making your app the default web2py application, they are detailed here in the section called ‘Application Init’. When an app is the default web2py app, you don’t have to specify its name in its url when you visit it in your browser.

 

Here is a summary of the method that I prefer.

 

First create a file in your web2py directory called routes.py, you can rename routes.example.py to routes.py but if you do, remember to delete any example code that it may contain.

 

Add the following code to routes.py:
[crayon]
routers = dict(
BASE = dict(
default_application=’your_app_name_here’,
)
)
[/crayon]
Replace your_app_name_here with the name of your web application, and the restart web2py. How you restart web2py depends on how you run it, for example in my case I restarted apache.

 

When web2py has restarted you should now be able to access your app via a naked url.

 

Twitter Bootstrap programmatically open or close an Accordion with javascript

Q: I have a nice accordion in my web app set-up in twitter bootstrap but how can I programmatically open or close a section using javascript?

A: Use the .collapse() function to open, close or toggle a collapsible accordion section.

Consider this 2 section accordion:

[crayon]

The camera settings

The acquisition settings.

[/crayon]
Then, as an example, to automatically open the second accordion section (identified by ‘#category-2’) when the page loads you could do the following:

[crayon]

[/crayon]
This technique can be used to automatically re-open a previously open accordion section on page refresh, to do this first use the history api to update the current page location whenever a user clicks to open an accordion section – store an id to the open section in the page arguments.

Then when the page loads, for example on a refresh, if this argument exists parse it from the page loaction and then call collapse(‘show’) to open the section…

More details on .collapse() can be found here.

 

Web2py and python – Change to model definition may break web site

In Web2py if you want to change a model’s definition, you can first of all change the model’s software definition via its python code and then, (depending on your migration settings), the next time you launch your web app, web2py will attempt to alter your database structure to match your updated model.

 

If this works, then it works and your database will reflect your new model, however sometimes web2py can’t make the necessary changes and your app may not subsequently load any more at all. If this happens then I have found the best way to fix the app is to delete all of the files in the app’s database directory – the next time you launch your app web2py will recreate your DB from scratch and it should launch again.

 

Now this is fine assuming that your database didn’t have loads of important data in it that you didn’t want to loose. So, to get around this problem, before trying to change a model’s definition I export all of the data in the database to a file (using a tool like this) so that if the problem happens and I have to get web2py to recreate the database ‘in its own image’ I can later reimport the previously saved data…