Web Development in Python: Lesson 1
By: Saranjeet Singh
Tags: Programming, Coding, Python, Django, Web Development

A quick tutorial to show the power of Django ( Python’s Framework) to create a website. So get your computer and start typing some code.

Anytime you do something with python, its a best practice to start with the virtual environment. The reason we use a virtual environment is to keep our dependencies in one place. As your project grows, you may need some other modules in your program, and imagine installing them every time you start your project after either you cancel your terminal or shut down your laptop 😳 So first go to the folder where you want to keep all your code files. Go into terminal and type this:

#create a directory

mkdir Project

cd Project #install virtual environment

python3 -m venv env

source env/bin/activate pip install django #to check if it is installed properly

python3 -m django --version #you should see something like this

#3.0.6

now, Django is installed, and let’s start cooking some code. The first command is to start the project

django-admin startproject website

so this is how your website folder looks like, let’s see what’s inside.

__init__.py: this is an empty file. It tells Python to treat this directory as a Python Package. asgi.py: will be used for deployment. settings.py: You will play with it a lot. urls.py: VERY IMPORTANT file.these are your controllers. wsgi.py: again, for deployment. website/manage.py: it’s a command-line utility. manage.py will be referred with nearly all the Django’s command.

This is the basic setup you get after you ran your first command. Now, imagine your website has e-commerce stuff, you handle payments, there are different pages showing different products. A lot of different coupons, sales, etc. and that can make your folder into a giant folder, and everything will be a mess. Django has a smarter way to do this, using Apps. For nearly every different feature, there will be a different app. Cool right?! 😎

To add an App to your website project, add this command.

python3 manage.py startapp homepage

Now we have another folder inside the website called homepage. It has some files:

__init__.py admin.py: we will use this file very soon when we will have our database ready. apps.py: this file has our App name defined. Our case its homepage. models.py: VERY IMPORTANT file. We will use this to defined our database. We will discuss this later in detail. tests.py: to right our tests. We will use it later as well. views.py: VERY IMPORTANT file. We will define our views here.

As you see we have 3 VERY IMPORTANT files, they when combine called MVC. Model View Controller. In the next lesson, we will learn more about MVC.

Enough of talking, now time to see some magic. I am using Visual Studio Code here, you can use any, ATOM, PyCharm, etc. Open your views.py file and type this code.

website/homepage/views.py

from django.http import HttpResponse def index(request):

return HttpResponse("welcome to my website")

create a urls.py inside homepage

website/homepage/urls.py

from django.contrib import admin from django.urls import path from homepage import views urlpatterns = [path('', views.index, name = 'index'),]

Now open website/urls.py

from django.contrib import admin

from django.urls import path, include urlpatterns = [

path('admin/', admin.site.urls),

path('', include('homepage.urls'))

]

your views.py file, website/homepage/urls.py, website/urls.py should be updated. Now run the following command:

python3 manage.py runserver

Go to 127.0.0.1:8000

woohoo, you can see your handwritten message:

go to 127.0.0.1:8000

well, your 1st lesson ends here. Next time, we will dive into the other basics of Django and will proceed slowly towards complexity.

Till then, goodbye :)

A note from Plain English

Did you know that we have launched a YouTube channel? Every video we make will aim to teach you something new. Check us out by clicking here, and be sure to subscribe to the channel 😎