Create First Project and Start Development Server -- Words (851)

Django

Now that we have installed required development tools including Django framework ,it's time for the first real step to start building our real estate app while learning Django essentials from scratch .

Django framework includes a bunch of very useful utilities to create and manage projects that can be accessed from a Python file called django-admin.py that becomes available when we first installed Django .

In this section we are going to see how to :

  • Create a new project
  • Setup and create the project database
  • Start the development server

Create a new Django project

Creating a new Django project is easy and quick so open your terminal or command prompt then enter :

$ django-admin.py startproject django_realestate

This command will take care of creating a bunch of necessary files for the project .

Executing the tree command in the root of our created project will show us the files that were created .

.
├── django_realestate
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py

init is the just the Python way to mark the containing folder as a Python package which means a Django project is a Python package .

settings.py is the configuration file ,you can use this file to specify every configuration option of your project such as the installed apps , site language and database options etc .

urls.py is a special Django file which maps all your web app urls to views .

wsgi.py is necessary for starting a wsgi application server .

manage.py is another Django utility to manage the project including creating database and starting the local development server.

These are the basic files that you will find in every Django project .Now the next step is to set up and create the database .

Setting up database

Using your favorite code editor or IDE open your project settings.py file and lets configure the database .

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

Django works with multiple database systems from simple to advanced systems (both open source and proprietary ) such as SQLite , MySQL ,PostgreSQL , SQL Server ,Oracle etc .Also you can switch to any database system whenever you want ,even after starting developing your web app ,without any problems thanks to Django ORM that abstracts how you can work with any database system .

For the sake of simplicity we'll be using SQLite since it comes already installed with Python so we actually have our database configuration already set up for development .Next for deployment you can use an advanced database system such as MySQL or PostgreSQL by just editing this configuration option .

Finally we need to tell Django to actually create the database and tables .Even if we didn't create actual code or data for our app yet ,Django needs to create many tables for its internal use .So lets create the database .

Creating the database and the tables is a matter of issuing this one command.

$ python manage.py migrate 

You should get an output like :

Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying sessions.0001_initial... OK

Since we are using a SQLite database you should also find a sqlite file under current directory :

.
├── db.sqlite3
├── django_realestate
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── urls.py
│   ├── urls.pyc
│   └── wsgi.py
└── manage.py

Starting the local development server

Django has a local development server that can be used while developing your project .It's a simple and primitive server which is suitable only for development not for production .

To start the local server for your project you can simply issue the following command inside your project root directory :

$ python manage.py runserver

Performing system checks...

System check identified no issues (0 silenced).
June 20, 2017 - 20:29:22
Django version 1.11.2, using settings 'django_realestate.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Next navigate to http://localhost:8000/ with a web browser .

You should see a web page with a message :

It worked!
Congratulations on your first Django-powered page.

Next, start your first app by running python manage.py startapp [app_label].
You're seeing this message because you have DEBUG = True in your Django settings file and you haven't configured any URLs. Get to work!

To conclude this section lets summarize what we have done throughout this tutorial . We have created a new Django project ,created and migrated a SQLite database and started a local development server .

In the next tutorial we are going to start creating our real estate prototype .



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.