In your terminal go to you web2py folder and create a new app called pitch:
$ cd /your/web2py/folder # on Windows use backslashes
This will create a new app pitch (unless there is allready one) and put you in a python shell (we come back to the shell later). For the moment press
Ctrl + D to exit the web2py python shell.
Putting things under version control
In the last chapter we installad the version controll system Mercurial; it's time to put it into action:
Again in the terminal go to the directory of the pitch app; create a repository; then create a .hgignore-file; add files to the repostitory and make your initial commit:
$ cd applications/pitch
add the following content to
.hgignore and close gedit
go back to the terminal
$ hg init # hg is the element of mercury in the periodic table
Pushing your code to BitBucket
Now we will push our code to bitbucket.org, a website specialized on hosting Mercurial repositories.
This has two advantages:
- You have a complete backup of your code.
- If you will get new team members, it will be much easier to collaborate.
First get an account on http://bitbucket.org, after that login and go to "Repositories" and "create repository".
Fill out the fields and click on "Create repository". Again in the terminal:
$ hg push https://email@example.com/your_username/pitch
Fill in your password and your code should be pushed to your bitbucket account.
What is the buzz about version control all about?
Probably it's not 100\% clear, why version control is so helpful. Let me give you an example. We will now delete the whole
controllers directory and then bring it back. In the pitch directory:
$ ls controllers/
This way Mercurial let's you code with confidence. If you break something, you can always go back. One good habit with version controll is to commit often.
Creating the models
Ok, now we have to create the user model and the post model. Good things first: web2py allready created a user model for us; for the tukker app we will create a customized one.
For the post model open the file
models/db.py and at the end of the file
fill in the following content:
What did we do? We defined a table with two fields; one text field and a reference field to the user model; the id field is automaticly created by web2py, there is no need to do this explicitly.
Now let's play around with what we created. Go to http://127.0.0.1:8000/pitch/default/user/register and register a user. After we registered our user we can login at http://127.0.0.1:8000/pitch/default/user/login or view the profile of our user at http://127.0.0.1:8000/pitch/default/user/profile.
Now we need an interface to create post with our user, but first let's commit our changes:
$ hg status
Creating a post form
To create a message form we must open the file
$ gedit controllers/default.py
create a function
post at the end of
now go to http://127.0.0.1:8000/pitch/default/entry_post and entry one or two posts, logout and again entry a post.
What have done? We created our first Python function. We will explain function later in more detail; just keep in mind that the first line of a function begins with
def and ends with a colon
:. The rest of the function must be indented.
In computer science, a subroutine (also called procedure, function, routine, method, or subprogram) is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code.
In the second line of the function we create the form to entry a post. We use the CRUD mechanism of web2py. It is a standardized way to create and manipulate database entries - web2py supports other ways, but we will go with CRUD.
In computer programming, create, read, update and delete (CRUD) are the four basic functions of persistent storage. Sometimes CRUD is expanded with the words retrieve instead of read or destroy instead of delete. It is also sometimes used to describe user interface conventions that facilitate viewing, searching, and changing information; often using computer-based forms and reports.
The last line transfers the form to the view.
Now let's logout and again go to http://127.0.0.1:8000/pitch/default/entry_post - you can still add posts. We will now add some user restrictions; add the following line to
controllers/default.py, but first commit your changes in the terminal:
$ hg commit -m"created basic entry form for posts"
go back to
Refresh the page; you should see a login form, after you login with your email and username you can post messages again.
As a final step we will show users and messages on the home page of our pitch app. But first, commit your changes:
$ hg commit -m"added authentication for post entry form"
Go to the
index function in the
default controller and change it like this:
At http://127.0.0.1:8000/pitch/default/index you can view all users and posts.
Again commit the changes:
$ hg commit -m"Show users an posts on the homepage"
Don't worry we will create a nicer homepage for the final tukker app.