Saturday, July 26, 2014

django - First project

If you have not checked the earlier posts about how to install Python and django, please go through these posts first before checking this:

Installing Python
Installing django

Let us start with creating a new folder 'Django Projects' in your root directory of the system.  This will help in locating your projects and applications easily and quickly.  You can do this either by using the Windows Explorer or the DOS prompt.  Then go into this directory. 

Now, enter the following command to create a new project.  Let us name this as 'mysite'.

django-admin.py startproject mysite

This creates a new folder 'mysite' and other few files required for running the application server to manage the admin web site and other settings.



Basically, the following file structure is created:

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py

So we have a main root folder 'mysite' and again a sub-folder 'mysite' with some files.

'django-admin.py' is a Python file and common-line utility of django for performing administrative tasks. 

Note the 'manage.py' file within the main 'mysite' folder. This is a thin wrapper around the 'django-admin.py' file and is used to perform many useful tasks like creating the database, application so on. 

The second inner folder 'mysite' is actual and main Python package for your project.  It contains  'settings.py' file for configuring the database, url path, time zone etc.  And the file 'urls.py' contains the set of urls for your application specifying the web server where to look for and load the relevant pages.

django comes with in-built development server as well as the database called 'sqlite3'. To start up the development server, enter the following command while in main 'mysite' folder:
python manage.py runserver


You should see this screen, if there are no errors found in the Python files created and compiled. Note the url specified here "http://127.0.0.1:8000". Open up any browser and go to this url to check whether the application server is working fine or not. 

If you get the following screen, then the application server is working fine.



Note that this application server has to be used only for development and test purposes.  In the production environment, you can install something more extensive like Apache web server.

If you want to change the default port '8000' to something else, you can specify that port at the time of running the server like:

python manage.py runserver 8080

This will set the port to '8080'.

Note that the development server automatically reloads Python code for each request as needed and so you don’t need to restart the server for code changes to take effect.  But, some actions like adding files or compiling translation files don’t trigger a restart, so you’ll have to restart the server in these cases.  

All the better if you restart the server every time you make any changes.

No comments:

Post a Comment