Django is one of the most famous web framework writen in Python. Django is free & opensource.
Other than django, there is also flask for web framework writen in python. So, what is the difference between django & flask? To know what is the difference between them, you can googling for details, but persoanally I prefer django, because django is easier, so we can build our website faster.
Without any more boring stories, let’s start create our first django project. In this tutorial we will use django version 2.1, this version is lastest version when i write this tutorial. To see up to date version, you can visit https://www.djangoproject.com/download/
First step, we need to create virtual environtment, this is optional step, you can skip this step, but i recommend you to doing this step, with virtual environtment you can see what exacly python library what you need for each project.
Macbooks-MacBook-Air:~$ virtualenv -p python3 env
(env) Macbooks-MacBook-Air:~$ source env/bin/activate
After create & activate virtual environtment using that command, your prompt will show env like this
Okey, your virtual environtment is active now. Next step, let’s install django using pip install command
(env) Macbooks-MacBook-Air:~$ pip install django
Once done, you can see any module installed on your virtual environtment using command pip list
(env) Macbooks-MacBook-Air:~$ pip list
— — — — — — — — -
You can see that django 2.1.7 is installed in your virtual environtment. Now let’s start create our first django project
(env) Macbooks-MacBook-Air:~$ django-admin startproject first_project
After making django project using command above, you will see directory created with the same name as project that you created before. In this case, the directory name will be frist_project.
Enter to that directory and now you can run your first django project
(env) Macbooks-MacBook-Air:~$ cd first_project/
(env) Macbooks-MacBook-Air:first_project $ 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.
February 14, 2019–08:02:58
Django version 2.1.7, using settings ‘first_project.settings’
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
You will see some warning, but you can ignoring them for now. Open your browser and navigate to 127.0.0.1:8000 or localhost:8000, you will see nice display indicate that you have start your first django project succesfully
In this tutorial, we will create very simple website that display hello world. I will cover another advanced topic in the next tutorial.
To create simple website display hello world, firstly you need to edit file urls.py like this
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
Next, let’s create views.py file
from django.http import HttpResponse
return HttpResponse(‘<h1>Hello World!</h1>’)
Now, if you navigate to localhost:8000 you will see something like this
But don’t worry, try navigate to localhost:8000/hello-world/, and you will see something like this
Congratulation, you have done your first django project. For another advance topic i will cover in other article, stay tune!