Getting Started With Django — 1(Beginner series)
New to Python? or already dancing some tango with it and you feel it’s time to dabble into some framework magic for web?
DJANGO is a web framework built entirely on python,it’s free, open-sourced and also follows the Model View Controller pattern, (in this case; Model, Template, View — Where the View relates to the Controller and The Template relates to the View in the MVC pattern. This part can be a bit confusing to newbies starting out, but along the line you’ll get to understand the logic behind this more :). There are already lots of popular sites running on django, examples are:
Django also provides some excellent documentation here,along with features and tools, some of which include:
- A nice templating language.
- Security features like CSRF
- Excellent lightweight server for development and testing e.t.c
In this tutorial, i will be showing you how to get your first Django website up and running. Before we start, we need to have Python downloaded and installed on our System, to download and install python, click here .
Note: You need to already have a basic understanding of python, also I’ll be running this tutorial on a Linux based system, so most commands would follow suite the linux way. But there will be little or no difference on most.
To ensure it’s fully downloaded, open up your terminal and type in
An interactive shell shows up:
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
To exit, type in
ctrl + z.
To have a more neater arrangement, it’s always advisable to create a directory for your projects
cd into the project with:
cd folder_name (every other step will be carried out while inside this folder)
Set Up Your Virtual Environment:
Next thing we need to do, is set up our virtual environment, a virtual environment helps you run several versions of python/django right on your machine (e.g you could have two different python/django projects running on different versions, to avoid them clashing and to give you room to run them both without errors, the virtual environment comes to your rescue. One virtual environment = one python/django version).It’s strongly advised to always use a virtual environment.
To set up our virtual environment, we’ll be using python’s package manager
pip to do the installation, type in:
pip install virtualenv
After installation,it’s time to create a virtual environment that would enable us use a preferred django version of our choice:
env_name should be replaced with the preferred name of your environment. (I like to name my environments with the django version installed in it for easier recognition).
Activating Virtual Environment:
To activate our virtual environment for linux/Mac OS:
Now it’s time to install django on to our machine:
pip install django==1.8
==1.8 only gives a direction to django about the particular version you want to install, in this case version 1.8. To just go ahead and download the latest version, input
pip install django .
Starting A project:
Now we have django up and running, it’s time to start up our first project! Yaah!. Still in our command line, type in :
django-admin.py startproject project_name
project_name = name of your project . In this case, we’ll work with
mask_off as our project name.
This creates a sub-folder with the name
mask_off and a skeleton structure of
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
Django gives us more ease by creating the above files:
1) the __init__.py helps python treat the directories as containing packages; so as to prevent directories with a common name, from unintentionally hiding valid modules that occur later (deeper) on the module search path.In most cases, it’s usually an empty file.
2) The settings.py file contains all settings your project requires, as we progress, we’ll visit this file often.
3) The WSGI (Web Server Gateway Interface) acts as the interface our web server uses to interact with our web application. Read more about it here.
There’s no fun thing as that of visiting your own webpage, so let’s run our server which also generate a link for us to view our webpage
python manage.py runserver
This shows up…
python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
July 12, 2017 - 15:19:01
Django version 1.8, using settings 'mask_off.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Notice the warning message about having unapplied migrations? Now let’s do a small but very important talk about migrations;
Migrations helps us make changes to our database schema without losing any data, each time we create a new model or make changes to a current one and run migrations, it helps update our database tables with the schemas without having to go through all the stress of dragging and recreating the database ourselves.
To make our migration:
python manage.py migrate
An output of this sort should show up:
Operations to perform:
Synchronize unmigrated apps: staticfiles, messages
Apply all migrations: admin, contenttypes, auth, sessions
Synchronizing apps without migrations:
Running deferred SQL...
Installing custom SQL...
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... 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 sessions.0001_initial... OK
This implies a successful migration, Now we can successfully run our server with no issues
python manage.py runserver . We get a success message on our webpage like the one below
Now we have our server running and a ‘webpage’ , but django takes pleasure in reminding us we still have a lot of work to do before we can proudly call this a webpage.
The second part of this tutorial would contain; Creating a new app in our project, working with Urls, Templates, Creating views and Linking pages. For now, take out more time to go through the steps again and again to become more familiar with them. Only practice makes perfect!
Things learnt From this Tutorial:
- Setting up a virtual environment(Downloading, Activiating Virtual env)
- Installing Django
- Creating a project
- Basic project components
- Running Server
Encountered any issue along the line? Let me know! I’ll be glad to help