How to Create Project in Django Python Framework



Installation and setup of python and Django

Django is a Python framework that is used to make python projects and web applications easily.It works for any python version. It supports a database called SQLite and you don't have to set up the database. For using Django you first need to install python. You can get python @ http://www.python.org.After installing python you can check whether Python is installed or not by typing python command in cmd, and if python is installed you will see the python version in the command line. After that, you can install Django's latest version and start making python projects. For the installation of Django you can refer https://docs.djangoproject.com.Now if you are done with installing python and Django you can check the version of Django you installed by typing and running the command

 python -c "import django;print(django.get_version())"


Creating a project in Django

Change your current directory where you want to store your code in the command line and then run the following command

django-admin.py startproject myproject  

This will create my project directory in your current directory. Remember not to use Django and test as your project name as these are the built-in names of Django and python components and cant be used. Inside your project directory you will see


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


Running up the server 

Now, let's check whether what we have done worked or not. To run the server you need to change it to my project directory and then run the command

python manage.py runserver 

You will get the following output in the command line


Validating models...

0 errors found
April 12, 2013 - 15:50:53
Django version 1.6, using settings 'myproject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

which means that our Django project is created successfully. Now you can copy-paste this URL in your browser and will see the "Welcome to Django" page. Sometimes it will show an error after you run runserver command and might show errors such as "socket is already in use".That means the port number you are using in URL is already used by some other server. So if you are getting this type of error you need to change your port to 8080. For that run the command: python manage.py runserver 8080.And now use the URL http://127.0.0.1:8080/ in your browser.


Database setup

By default, Django uses the SQLite database. If you are new to the databases you should prefer to use SQLite.SQLite is installed in python so you did need to install anything. Now go to your project directory and inside that open settings.py file and edit the connection settings.

Engine- 'django.db.backends.sqlite3'
Name- Full path to your myproject directory.For example if it is in C drive then enter 'C:/myproject/sqlite3.db'

If you are using SQLite as a database then you can leave the fields username, password, host, port empty. Also, you need to change the Time_zone field to your timezone. You can also see the Installed_apps field in settings.py file where you can see the Django applications which are present by default in the code.


INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    )

Each of these applications requires database tables. So it is necessary to create tables for them before using them. For creating tables run the command

 python manage.py syncdb 

syncdb command will create necessary tables for the applications in installed apps. You will see the messages while creating the tables and you will get a prompt to create a superuser, type yes, and then enter a username, email, and password to create a superuser.

In the next post, I will be writing about how to create models and create views and URLs in Django for creating your first web application.


Comments

Popular posts from this blog

Tricky Questions or Puzzles in C

Program to uncompress a string ie a2b3c4 to aabbbcccc

Number series arrangement puzzles