Python Tutorial: Django Tutorial for Beginners

Django is a Python-based, free, open-source web framework. It follows the Model, View, Controller (MVC) architecture. it designed by Adrian Holovaty and Simon Willison on July 2005. In this tutorial, (Django Tutorial for Beginners) we focus on Django basic installation and introduction.

Documentation: https://docs.djangoproject.com/en/2.2/

What is Python?

Python is a scripting language and that is used for creating engineering-analysis tools, web development (server-side), software development, system scripting, and animation software. It designed by Guido van Rossum and first released in 1991. It’s an interpreted, High-level. General-purpose language. Python runs on Windows, Mac OS, and Linux.

Python Tutorial: Introduction for Beginners (Part 1)
Python Tutorial: Introduction for Beginners (Part 2)
SQL Tutorial: Introduction for beginners (Basic)
Python Tutorial: How to Connect MySQL Database in Python
Python Tutorial: Python GUI Programming (Tkinter)
Python Tutorial: Send Emails Using Python

What is MVC?

Model – This component used for the define our database tables and organize data.
View – This component contains the UI logic in the Django architecture.
Controller – This component handles the user interaction and selects a view according to the model.

What is MVT?

Model, View, Template (MVT) architecture
The main difference between the MVC and MVT patterns is that Django takes care of the Controller part itself. leaving the developer with the template.

PS: Django supports the MVC pattern and MVT pattern both but Django has been referred to as an MTV framework because the controller is handled by the framework itself.
Check here >>

Django Tutorial for BeginnersInstallation

Enter the following command in cmd and press enter

pip install django

Start Django Project

Enter the following command in cmd and press enter

django-admin startproject projectName

Then open Project Folder using your favorite code editor.

How to install Code Editor on Windows

Python in Visual Studio Code: https://code.visualstudio.com/docs/languages/python

Start Test Server

Enter the following command in cmd and press enter

python manage.py runserver

Then open a web browser and visit 127.0.0.1:8000 or localhost:8000 (This is the test server page on the web browser)

Creating the Blog app

Enter the following command in cmd and press enter

python manage.py startapp blog

Projects vs Apps:

“An app is a Web application that does something (ex: a Weblog, a database or a simple poll app. A project is a collection of configuration and apps for a particular website. A project can contain multiple apps. An app can be in multiple projects.”https://docs.djangoproject.com/en/2.2/intro/tutorial01/

It will create a new directory called a blog

Create a new View

Open the file blog/views.py and enter the following code.

import

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

# Create your views here.
def vMain(request):
    return HttpResponse("<h1> Hello World! </h1>")

To create a URLconf in the blog directory, create a file called urls.py

import

from django.urls import path
from . import views

Then enter the following code and save urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.vMain, name='main-page'),
]

To point blog URLconf to the root URLconf. open firstProject/urls.py

import

from django.urls import include

Then enter the following code and save urls.py

from django.contrib import admin
from django.urls import path
from django.urls import include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('blog/', include('blog.urls')),
]

Now run a project using the following command and visit 127.0.0.1:8000/blog

python manage.py runserver

Connect MySQL Database with Django

What is SQL?

SQL (Structured Query Language) is a standard language for storing, managing and retrieving data in databases. SQL was developed at IBM by Donald Chamberlin and Raymond Boyce. and first released in 1991. SQL became a standard of the ANSI (American National Standards Institute) in 1986.

SQL Tutorial: Introduction for beginners (Basic)

In this tutorial, we use XAMPP {Apache (A) + MariaDB(M) + PHP(P) + Perl(P)}

CtechF - How to install XAMPP - Screenshot

XAMPP is an Open source software. It’s a Stands of Cross-platform (X). we can create a local web server using XAMPP. It’s easy to use and it works with Windows, Linux, and Mac OS. You can use alternative software for XAMPP, such as WAMP, MAMP.

XAMPP Download link (121 MB) – https://www.apachefriends.org/index.html
WAMP download link (286 MB) – http://www.wampserver.com/en/#download-wrapper
MAMP download link (410 MB) – https://www.mamp.info/en/downloads/

Install XAMPP

Click on the downloaded XAMPP setup. (If you got any permission message or warning message click “OK” or “YES” button to continue) It will open the XAMPP setup wizard. Click the next button to continue.

CtechF - How to install XAMPP - Screenshot

Now it asked, what components we want to install and what’s not. We only need Apache and MySQL. (select following Components and click the next button to continue)

  • Server
    • Apache – Apache Server
    • MySQL – Database Server
  • Program Language
    • PHP – Hypertext Preprocessor
  • Program Languages
    • phpMyAdmin – Database Control panel
CtechF - How to install XAMPP - Screenshot

Choose installation location and click Next (Default: C:xampp)  (If you got Windows Defender Firewall Permission request, Tick both checkboxes and click Allow access.) Then click the next button to continue Now it will install XAMPP (wait till the installation complete) After the installation complete. Click Finish.

It will open the XAMPP Control panel automatically. This is a XAMPP Control Panel.

CtechF - How to install XAMPP - Screenshot

Now click Start Apache and MySQL (It will start Apache and MySQL servers)

CtechF - How to install XAMPP - Screenshot

Create a Database

Open a web browser and type http://localhost/phpmyadmin or http://127.0.0.1/phpmyadmin (it will redirect you to phpMyAdmin control page)

CtechF - How to create WordPress blog using xampp (Localy) - Screenshot

Click Databases and enter the Database Name then click Create, it will create our database.

My database name is “dbpy_firstproject”

Database Configuration – settings.py

Installation

Microsoft Visual C++ 14.0 is required. Get it with “Microsoft Visual C++ Build Tools”: https://visualstudio.microsoft.com/downloads/
Then install Windows 10 SDK (10.0.17763.0)

python -m pip install mysqlclient

if you had a problem while installing mysqlclient check following document
https://python-forum.io/Thread-error-while-executing-pip-install-mysqlclient

Open the file called, firstProject/settings.py and find this part

# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases

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

Enter the following code and save setting.py

DATABASES = {
    'default': {
      'ENGINE': 'django.db.backends.mysql',
      'NAME': 'database_Name',
      'USER': 'database_Username',
      'PASSWORD': 'database_Password',
      'HOST': 'your_hostname',
      'PORT': 'port',
      'OPTIONS': {
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
       }
     }
}

Enter the following command in cmd and press enter to migrate the database.

python manage.py migrate

Github: https://github.com/ctechf/django_tutorial/

Part 1 – Completed

Sources : 
https://pixabay.com/ (images)
https://docs.djangoproject.com/en/2.2/ (Documentation)

Hello Guys,

Hope this post (Python Tutorial: Django Tutorial for Beginners) will help you to learn something, If you enjoy my work then please share my posts with your friends and anyone who might be interested in web development and don’t forget to subscribe my mailing list.

If you had any problem with this post, please do mention it in the comment section.

Like ctechf Facebook fan page : https://www.facebook.com/ctechf

Also Read :

How to be a Programmer

Liked it? Take a second to support CtechF on Patreon!

Leave a Reply

506 Shares
Share
Tweet
Share
Pin
Share