Django 1.11 : Create or start your first project
Django is a Python based framework which offers developers all they need to create a web apps and websites in a clean ,rapid and a pragmatic way .
How do I start a Django project ? is the first quesion which gets asked by a beginner Django developer so lets answer it .
To better grasp this tutorial ,you need to have some Python experience and also how to work with Linux bash commands or Windows command prompt if you are using Windows for development .
Before you can create or start a Django project you need to have you development environment setup .
Here is what you need to install before continuing with this tutorial :
Python 2.7.x or 3.4.x
PIP package manager
Code editor (Sublime,gedit etc.)
If you are using a Linux/MAC machine you should have Python already installed .
You can check that running the following command on your terminal
If you don't have Python installed head over to Python download page
PIP is a Python package manager used to easily install Python packages and their dependencies .You can install PIP using curl utility
curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py" python get-pip.py
Then verify It's successfully installed
Virtualenv is a tool which allows you to create virtual Python environments ot sandboxes .It's a common practice to create a virtual environment for each Python project to prevent different versions of packages and libraries from messing with each other .
To install Virtualenv ,run the following :
pip install virtualenv
After installing all development requirements .It's time to create a virtual development environment using Virtualenv :
mkdir django111-project cd django111-project virtualenv env
Then make sure to activate the environment with :
In case you want to exit the environment ,you simply run :
Now we are ready to install Django .
pip install django
This will install the latest Django framework version .
Create/start Django project
At this point after installing the development environment ,setting up a new virtual environment and installing the latest version of Django .You can create and start a django project by running :
django-admin.py startproject django111-project
This will create a project with a basic directory structure
├── django111-project │ ├── __init__.py │ ├── settings.py │ ├── urls.py │ ├── wsgi.py └── manage.py
Next navigate inside your project :
Setup a database using :
python manage.py migrate
This will create a SQLite database which is the default option for Django project but you can also use any other advanced database system such as MySQL or PostgresSQL etc .
SQLite comes pre-installed with Python so we are going to use it for this simple project .
Then run a local development server with :
python manage.py runserver
You should be able to visit your app at :
Create a Django app
A Django app is a collection of apps which are used to separate logical units of your app for better project organization .
To implement any project requirements you need first to create a Django app :
python manage.py startapp app1
Your project directory structure becomes
├── app1 │ ├── admin.py │ ├── apps.py │ ├── __init__.py │ ├── migrations │ │ └── __init__.py │ ├── models.py │ ├── tests.py │ └── views.py ├── db.sqlite3 ├── django111-project │ ├── __init__.py │ ├── __init__.pyc │ ├── settings.py │ ├── settings.pyc │ ├── urls.py │ ├── urls.pyc │ ├── wsgi.py │ └── wsgi.pyc └── manage.py
But not just that ,you need also to do some configuration .
Head over and open your project settings.py ,locate INSTALLED_APPS and your app there .
INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.admin', 'app1', )
Next you need to create migrations files :
python manage.py makemigrations
Then actually run migrations to create tables related to the second app
python manage.py migrate
So we have seen how to create and start a new Django project by first installing the development environment then create the actual project .