Django 1.11 : Create or start your first project -- Words (794)

Django
Django 1.11 create/start 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 .

Tutorial requirements


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 .

Development requirements


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

Virtualenv

Django

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

python -v 

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

pip -V

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 :

source env/bin/activate

In case you want to exit the environment ,you simply run :

deactivate

Now we are ready to install Django .

pip install django

This will install the latest Django framework version .

When writing this tutorial the latest Django version is 1.11

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 :

cd django111-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 :

http://127.0.0.1:8000/

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 

Conclusion


So we have seen how to create and start a new Django project by first installing the development environment then create the actual project .



mrnerd is a Web Entrepreneur & Developer, Digital Marketer and a Passionate Blogger. He writes about software and website development,money making tips and SEO etc.. He is a tech addict and a computer geek with a lot of hobbies.