Top  Next

Creating a dynamic web site... with 2 lines of code

Dynamic web sites are generally associated to a database : a catalog of products, a list of users, the messages in a forum, etc. The site generally shows the contents of the database and allows all or some users to add, modify or remove items

To create such a site in its simplest form, you will have to write 2 lines of Python code. Yes, only two lines ! The script which will manage the database and allow you to add, edit or remove records is generated by a program, and you will be able to edit it so that it adapts more closely to you needs

Let's get started. Open a text editor and create a Python script with these two lines :

name = "books"
fields = ["title:str","year:int","author:str"]

As you can imagine, this describes a (very simple) database of books. The base is called "books" and each book has three attributes : a title, a year of publication and an author. Title and author are strings and the year is an integer

Save the script in the directory called instant_site in your Karrigell distribution. Now open a console window, go to the directory admin and run the script called makeScript.py :

This will open a window, asking you to choose a configuration file

Double-click on booksConfig.py, then get back to the web browser and type http://localhost/instant_site/books.ks in the address field. Here is what you should see :

You will probably want to follow the link "New record", enter a book you like :

After you have clicked "Ok" you see that the list has been updated :

Now you can edit or remove the existing record, and add new ones

In the next step of the tutorial we will see how to add user management to this site. It will only take one more line of code !