Django Web Development Series: 1

Kuldeep Luvani
Nov 1 · 4 min read

I am a professional data scientist and entrepreneur. Trying to learn some web development in Python. I love Python. I have done most of my development in Python. Apologies in advance if I make any mistake in the tutorial.

In this tutorial, we will be covering everything you should need to get started with web development. Here we will create a website which requires:

  1. Database
  2. Users
  3. Content manager
  4. Dynamic pages and more!

Django is a very high level and fully featured web framework. It aimed at allowing you to achieve rapid development while also preparing you from the start to create a website that will scale in time, both in terms of speed and code.

Before we begin, I assume you have basic knowledge of python 3, if not I suggest follow any python tutorial on youtube. (Assuming you are using Linux, You can also use similar in Mac and Windows) You can create a virtual environment using virtualenv or Anaconda (I am using Anaconda here. You can find Anaconda Tutorial here). Activate the environment and run below command:

mluser@mluser-ThinkPad-T450:~$ conda activate django_tut

Install Django

(django_tut) mluser@mluser-ThinkPad-T450:~$ pip install django
Collecting django
Downloading https://files.pythonhosted.org/packages/b2/79/df0ffea7bf1e02c073c2633702c90f4384645c40a1dd09a308e02ef0c817/Django-2.2.6-py3-none-any.whl (7.5MB)
|████████████████████████████████| 7.5MB 3.2MB/s
Collecting sqlparse
Downloading https://files.pythonhosted.org/packages/ef/53/900f7d2a54557c6a37886585a91336520e5539e3ae2423ff1102daf4f3a7/sqlparse-0.3.0-py2.py3-none-any.whl
Collecting pytz
Downloading https://files.pythonhosted.org/packages/e7/f9/f0b53f88060247251bf481fa6ea62cd0d25bf1b11a87888e53ce5b7c8ad2/pytz-2019.3-py2.py3-none-any.whl (509kB)
|████████████████████████████████| 512kB 1.5MB/s
Installing collected packages: sqlparse, pytz, django
Successfully installed django-2.2.6 pytz-2019.3 sqlparse-0.3.0
(django_tut) mluser@mluser-ThinkPad-T450:~$

Once It is installed successfully, we will create new projects with a name mywebsite < you can use the name you like for your project>. To access the django framework, we can use keyword django-admin. Django-admin keyword gives all sorts of ability to create a new project, run the server, migrate, sqlflush and many more.

(django_tut) mluser@mluser-ThinkPad-T450:~$ 
(django_tut) mluser@mluser-ThinkPad-T450:~$ django-admin startproject mywebsite
(django_tut) mluser@mluser-ThinkPad-T450:~$

It will create a new repository named mywebsite. That will be your project. Below will be your directory structure in mywebsite.

(django_tut) mluser@mluser-ThinkPad-T450:~$ tree mywebsite/
mywebsite/
├── manage.py
└── mywebsite
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
1 directory, 5 files

Go to mywebsite directory and start the app (here I am creating app name main, you can use whatever app name you wants to make) using below command:

python manage.py startapp main

It will create the main app in your mywebsite project. It will create another directory name main.

(django_tut) mluser@mluser-ThinkPad-T450:~$ tree mywebsite/
mywebsite/
├── main
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── manage.py
└── mywebsite
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-36.pyc
│ └── settings.cpython-36.pyc
├── settings.py
├── urls.py
└── wsgi.py
4 directories, 14 files

If you observe, the main directory looks a lot like mywebsite directory. Now we will be running the server using manage.py file.

(django_tut) mluser@mluser-ThinkPad-T450:~/mywebsite$ python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 17 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.

October 30, 2019 - 18:02:53
Django version 2.2.6, using settings 'mywebsite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Dont worry about the error for migration. We will take care of that in later example. Now our server is running on localhost 8000 port, so open https://127.0.0.1:8000/ in your browser, you will see a page like this:

Let's get into mywebsite url.py because whenever someone accesses the app, it checks the pattern in mywebsite url.py. Open url.py into a text editor and point out mywebsite to the main app.

from django.contrib import admin
from django.urls import path, include #new

urlpatterns = [
path('admin/', admin.site.urls),
path('', include('main.urls')), #new will point to main.urls

]

If you observe, in main folder, you dont have url.py file, so copy the url.py file from mywebsite to main folder.

Now open url.py from the main folder.

from django.urls import path
from . import views #relative import for views

app_name = "main" #our main app is "main"

urlpatterns = [
path("", views.homepage, name="homepage"), #blank url should redirect to homepage
]

Now we will update veiws.py and create a homepage.

from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
def homepage(request):
return HttpResponse("My first Django website. Its <strong>cool</strong> website")

Now again open the https://127.0.0.1:8000 and refresh the page, you will see the Http response.

Walah! We have successfully installed Django and started our first web app. Let me know in the comment section if you have any queries in this tutorial.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade