Today, I am going to delve into another framework. This time it deals with the python programming language. As far as I know there is one huge web framework based on this language. The framework calls itself “Django”. Its website describes itself as a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Well I’m not sure about you but it at sounds pretty appetizing. Hopefully in this walk through we will learn about another tool that you can add to your already full web developer toolbox.
I am going to assume you have got Django installed on your server and are chomping at the bit to learn this highly touted web framework. I will be doing my work from a shell using OpenSSH software, and using the Vi editor. Below is a list of some simple Vi commands to help you get started if you have never used it before. A whole post could be dedicated to vi
i = insert mode
ESC = command mode
:w = write / save file
:q = quit
Alright, the first step in getting a project going is to create the files needed for one. Navigate to your django projects directory and type in the following command:
$ django-admin.py startproject myproject
This creates a new folder called “myproject” in the current directory. This is where the majority of your coding will happen. Move into this directory now type in the following:
vi settings.py
This will open up the settings.py file which you can change the settings of all applications underneath this project. The one we will worry about right now is our database information. Create a database on your server that you can use and then fill out the DATABASE_ENGINE / DATABASE_HOST etc. I will be using a MySQL database called django. Those are the only values you need to touch at the moment.
Now let’s fill this database with the information that Django requires to get started. Run the following:
python manage.py syncdb
Our basic project is now setup. It doesn’t do anything yet but it has access to a database. You should be able to access your public Django folder (one with dispatch.fcgi) and get some indication that your project is being executed. Ignore any errors at this point as we haven’t yet completed anything to be visible.
Now we want to start an application underneath this project. This is where the meat of the functionality of our application goes. Create a new application called blog (see below) in the /myproject folder.
python manage.py startapp blog
We have a directory called blog now. If you do run the ls command. You see all the files created:
$ ls __init__.py __init__.pyc models.py views.py views.pyc
The two files you will do a lot of modifying in will be models.py and views.py. Remember the acronym MVC, well those two files help accomplish that. For now let’s vi the views.py file and enter the following:
from django.http import HttpResponse def index(request): return HttpResponse(‘web design is fun');
If you are new to the python syntax this might be a bit greek to you, but really it is quite handy. Structures are not defined by curly braces but instead by spaces/tabs which are required. In line #1 we are telling Django to import the HttpResponse object from the django.http module which is part of Django’s code base.
Once we have imported this we can use this object. Views require us to accept a request and return an HttpResponse. In the above example all we are doing is showing the text “web design is fun”. Our job is not quite over however. Where do we go to view this text in the browser. Well we would like to make this our default view and go to, http://yoursite.com/myproject/.
Well in order to do this we have to map the /myproject portion to our view. Go back into the /myproject folder and vi the urls.py. This file is where all your front end urls will be. They use regular expressions, so if you are unfamiliar you can check out regular-expressions.info. For now all we need is to add the following:
(r'^myproject/', 'myproject.blog.views.index'),
This tells Django that going to http://yoursite.com/myproject/ will map to “myproject” then our blog application, down into that applications views, and finally run the index function. This corresponds to the index function we have created above. One last thing you may need to do is kill the currently running python process using the following command:
pkill python
Now go to that particular url we defined above and you should see that “web design is fun” has been printed in your browser window. We have just setup the starting blocks of our blog. If enough interest is expressed in Django, I will continue along this train of thought and begin creating models which will store our database information. For now, If you wish to take on a longer tutorial please check out the frameworks beginner’s tutorials.
If you like this post, please share it on your favorite sites. If you have your own website and want to link to me, the following is some code you can use.
<a href="http://blog.brenelz.com"> <img width="16" height="16" style="border: medium none ; position: relative; top: 3px;" src="http://www.brenelz.com/images/brenelz-ico.gif" alt="Brenelz's Web Tips"/>Brenelz's Web Tips </a>
2 Comments
“One last thing you may need to do is kill the currently running perl process using the following command”
i think you mean Python.
@frank - oops… thanks for pointing that out