Project in Django

D:\lab\batch28>pip install django

 

In Django, every web app you want to create is called a project;

and a project is a sum of applications.

An application is a set of code files relying on the MVT pattern. As example let's say we want to build a website, the website is our project and, the forum, news, contact engine are applications. This structure makes it easier to move an application between projects since every application is independent.

Create a Project

Whether you are on Windows or Linux, just get a terminal or a cmd prompt and navigate to the place you want your project to be created, then use this code −

$ django-admin startproject myproject

D:\lab\batch28>django-admin startproject myproject

This will create a "myproject" folder with the following structure −

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

The Project Structure

The “myproject” folder is just your project container, it actually contains two elements −

  • 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 −
$ python manage.py help
  • The “myproject” subfolder − This folder is the actual python package of your project. It contains four files −
    • __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': 'database.sql',
      '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.

You can also set others options like: TIME_ZONE, LANGUAGE_CODE, TEMPLATE…

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

D:\lab\batch28\myproject>python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).

You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
September 16, 2018 - 15:12:48
Django version 2.1.1, using settings 'myproject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

 

A project is a sum of many applications. Every application has an objective and can be reused into another project, like the contact form on a website can be an application, and can be reused for others. See it as a module of your project.

Create an Application

We assume you are in your project folder. In our main “myproject” folder, the same folder then manage.py −

$ python manage.py startapp myapp

You just created myapp application and like project, Django create a “myapp” folder with the application structure −

myapp/
   __init__.py
   admin.py
   models.py
   tests.py
   views.py
  • __init__.py − Just to make sure python handles this folder as a package.
  • admin.py − This file helps you make the app modifiable in the admin interface.
  • models.py − This is where all the application models are stored.
  • tests.py − This is where your unit tests are.
  • views.py − This is where your application views are.

Get the Project to Know About Your Application

At this stage we have our "myapp" application, now we need to register it with our Django project "myproject". To do so, update INSTALLED_APPS tuple in the settings.py file of your project (add your app name) −

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

3. Start App

D:\django\myproject>python manage.py startapp myapp

 

 

############################

Install django
pip install django

1. Create project
D:\django>django-admin startproject myproject

Dont change(edit) manage.py

D:\django>django-admin startproject website

2. D:\django>cd myproject

3. Start App

D:\django\myproject>python manage.py startapp myapp

4. Create Super user D:\django\myproject>python manage.py createsuperuser

######################