article

Sunday, June 11, 2017

Python Django - Install Create project and run

Python Django - Install Create project and run

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. Django makes it easier to build better web apps quickly and with less code.

You can download the latest version of Django from the link http://www.djangoproject.com/download 

Windows Installation

C:\>pip install Django==1.9.7
c:\>python -c "import django; print(django.get_version())"

Create a Project
c:\>django-admin startproject myprojectdjango

This will create a "myprojectdjango" folder with the following structure −
myprojectdjango/
   manage.py
   myproject/
      __init__.py
      settings.py
      urls.py
      wsgi.py

manage.py − This file is kind of your project local django-admin for interacting with your project via command line (start the development server, sync db...). To get a full list of command accessible via manage.py you can use the code −
__init__.py − Just for python, treat this folder as package.
settings.py − As the name indicates, your project settings.
urls.py − All links of your project and the function to call. A kind of ToC of your project.
wsgi.py − If you need to deploy your project over WSGI.

Setting Up Your Project
Your project is set up in the subfolder myproject/settings.py. Following are some important options you might need to set −

DEBUG = True

This option lets you set if your project is in debug mode or not. Debug mode lets you get more information about your project's error. Never set it to ‘True’ for a live project. However, this has to be set to ‘True’ if you want the Django light server to serve static files. Do it only in the development mode.

DATABASES = {
   'default': {
      'ENGINE': 'django.db.backends.sqlite3',
      'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
      'USER': '',
      'PASSWORD': '',
      'HOST': '',
      'PORT': '',
   }
}

Database is set in the ‘Database’ dictionary. The example above is for SQLite engine. As stated earlier, Django also supports −

MySQL (django.db.backends.mysql)
PostGreSQL (django.db.backends.postgresql_psycopg2)
Oracle (django.db.backends.oracle) and NoSQL DB
MongoDB (django_mongodb_engine)
Before setting any new engine, make sure you have the correct db driver installed.

Now that your project is created and configured make sure it's working −

c:\>myprojectdjango>python manage.py runserver

Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Related Post