• Skip to main content
  • Skip to primary sidebar

Technical Notes Of
Ehi Kioya

Technical Notes Of Ehi Kioya

  • Forums
  • About
  • Contact
MENUMENU
  • Blog Home
  • AWS, Azure, Cloud
  • Backend (Server-Side)
  • Frontend (Client-Side)
  • SharePoint
  • Tools & Resources
    • CM/IN Ruler
    • URL Decoder
    • Text Hasher
    • Word Count
    • IP Lookup
  • Linux & Servers
  • Zero Code Tech
  • WordPress
  • Musings
  • More
    Categories
    • Cloud
    • Server-Side
    • Front-End
    • SharePoint
    • Tools
    • Linux
    • Zero Code
    • WordPress
    • Musings

Introduction To The Django Web Framework – Creating A Simple Application

Tagged: Backend, Django, Python, Web application, Web framework

  • This topic has 0 replies, 1 voice, and was last updated 11 months ago by IdowuIdowu.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • February 10, 2020 at 5:15 pm #85571
    Idowu
    Participant
    @idowu

    Thumbnail

    Django is one of the most popular web frameworks for Python. It is commonly referred to as the “framework for perfectionists with deadline”. As an open source and free high-level web framework, Django allows you to build web applications faster and with less code – without having to worry about the baseline configurations needed to make your applications scale.

    Another popular web framework for Python is Flask. But there are many others.

    Just like other programming languages, Django is a framework which is used to make the backend of a web application with Python.

    What is a Frame Work?

    Simply put, a framework is a set of already made and customizable combination of packages and components which allows the easy deployment of complex tasks, thus allowing us to focus more on productivity, rather than creating all components and packages from the scratch.

    Advantages and Shortcomings of Django

    Some of the advantages of using Django Web framework includes:

    1. It allows rapid development of applications, without the developer having to worry about the initial configurations needed to run the project.
    2. It has an inbuilt csrf security which comes with the forms.
    3. It offers a higher prospect of scalability.
    4. Less codes with higher productivity.
    5. It provides easy connectivity to databases by allowing easy migration of models.

    Shortcomings

    1. Although, Django provides you with a wide range of packages and components, you might have a lot of navigations to make while using it.
    2. It could become too overwhelming and technical to deal with, especially for starters.

    Most of the popularly known available web frameworks work with the concept of Model, View, Controller (MVC). In Django, the normal MVC (Model, View, Controller) concepts has been replaced with the MVT (Model, View, Template) concepts.

    Model, View, Templates (MVT) in Django

    The model allows us to be able to create a table in the database – holding predefined fields which can hold specific objects, and as such, it’s basically used to handle data.

    Views in Django’s MVT are not synonymous to those of MVC which are usually in HTML formats. However, in Django, they’re encoded in a file called views.py, which is where functions to be rendered out in templates for dynamism are made.

    Templates are the HTML pages where most of the dynamisms on the views are rendered out for users’ experience.

    So, it’ll be correct to state that the Controller has been replaced with Views, while Views are being replaced with Templates in Django.

    Basically, what Django does is – it allows us to create models, define some functions in the views, which probably communicates with the models and render out the views on our templates.

    Basic Requirements Prior to Creating your App

    Before you can start creating your own Django project and building Web applications in them, you’ll definitely have to put some things in place, without which you’ll not be able to forge ahead.

    Let’s take look at the basic things you’ll need:

    1. I want to assume that you have Python installed on your local machine – if not, you can download Python by visiting the Python download page. This tutorial is based on Python version 3.7.
    2. A virtual development environment – this is necessary to manage dependencies and work in an isolated environment. If you don’t have the prerequisite knowledge about this, I’ve put up an article to help you through all the steps you need to take in order to create a virtual environment on Windows OS and also pip install packages.
    3. I will also assume that if you’re viewing this tutorial, you’re already familiar with the command line. If you’re not, not to worry, I’ll show you the basic commands you’ll need first off, as we move on with this article.

    Create a Django Project

    To be able create apps (startapp), first off, you’ll have to create a Django project, which must be within the virtual environment.

    For the purpose of this article, I’ll be making use of the virtual environment wrapper which I find very easy to use. You can choose to use other methods in your own case, it’ll not affect the productivity in any way.

    I also want to assume that you have pip package installed in the script directory of your home based Python version, if you don’t know how to go about that, you can refer to this article as I don’t want to digress too much.

    Now, let’s create a virtual environment with the name ”Tutorial”.

    Open up the command line in any folder directory on your PC, in my case, I’ll be creating a project in the “Python Project’s” directory on my machine.

    pic 1

    Within the command line, create a virtual environment by using the command; mkvirtualenv Tutorial

    pic 2

    By now, you should’ve been in the virtual environment. This is simply verified by ensuring that “Tutorial” is in parenthesis within the command line.

    pip install django by using the following command on your command line:

    pip install django, this will install the latest version of Django in your environment. You can as well decide to install a specific version of Django by using pip install django==2.0.7, which we’ll be using in this tutorial.

    Pic 4

    Use django-admin.py startproject tutorial to create a project with the name “tutorial”. Your project could have the same name with your virtual environment, it doesn’t matter.

    Pic 4

    Verify by locating the folder “tutorial” in the directory where you created it.

    pic 5

    Note that after creating a Django project, you can use any IDE to manage your project, one of such popular IDEs is Sublime text. You can use any IDE of your choice, as far as you know your way around it.

    In this tutorial, I’ll be using the Sublime text. But before we open it up, make sure that you run cd tutorial into the tutorial project. Then use the command python manage.py startapp my_firstapp to start a new application under the directory of the project we created earlier. You can give your application any name you wish.

    Now let’s open up the sublime text and import the project which is now containing the application “my_firstapp”.

    pic 6

    Select the file “tutorial” in order to work on it in the sublime text. Within the “tutorial” folder, you’ll see the application my_firstapp which we created earlier.
    Let’s take a look:

    pic 7

    Within each of the files, you’ll see some already built in components, all of which have their specific roles.

    Notable files which you’ll find within the my_firstapp file are:

    • models.py – which houses the models and relates with the database.
    • apps.py which houses your main application (my_firstapp).
    • views.py which will be used to configure the responsiveness on the templates.

    Whereas, within the project folder (tutorial), you’ll find notable files like:

    1. urls.py which will allow you to effectively manage your application’s urls.
    2. Another very important file is the settings.py, which houses the entire settings of your project.

    Now that you’ve started an app, it’s very necessary to add it to the list of INSTALLED_APPS within the settings.py by typing my_firstapp as shown below. Ensure that you add the comma after adding my_firstapp:

    pic

    Also, if on Windows OS, always ensure to hit on save or Cntrl S after each operation, I’m not familiar with mac OS.

    Create a folder called “templates” within the project directory as described below:

    • In the sublime text file manager, right click on ”tutorial” and select New folder

    pic 9

    Important: Go to settings.py and locate the name TEMPLATE. Also, locate DIRS:[]

    Inside the list parenthesis, type the following code to add your templates to the base directory of your project:

    1
    os.path.join(BASE_DIR, 'templates')

    os.path

    This step was necessary so that your templates can be seen by Django as html formats

    Now you’re almost ready to create your first running application with Django

    Please note: This tutorial will not cover the creation of models and databases. However, to manage and connect to the database, Django provides an inbuilt light weight SQL called “mysqlite”, which you’ll find by migrating into the “settings.py” file and checking the name DATABASES.

    pic 10

    After the steps above, within the my_firstapp, go back to views.py and create a view – we’ll be creating a simple app that performs a mathematical operation.

    1
    2
    3
    4
    5
    6
    7
    8
    # Create your views here.
    from django.shortcuts import render
    def add_nums_view(request):
    a=10
    b=20
    added=(a+b)**3
    context={'added':added}
    return render(request, 'result.html', context)

    I will also assume that if you’re reading this article, you have the basic knowledge of Python. The only probable strange thing here are the words render and request.

    Simply put, the request helps you to manage request by helping to call out to a particular Web page. Render on the other hand allows the operations performed under the views to be seen on the requested page.

    Run python manage.py runserver in command line to start the Django server, copy the server port and launch it in your browser:

    pic 12

    When you copy the port address in the command line above, paste it in your browser’s console and click on ok. You should receive a response typical to what’s found below:

    pic 13

    Now, the Django server is running.

    Migrate back into the sublime text and locate the urls.py, within it, import and include the views add_nums_view:

    1
    2
    3
    4
    5
    6
    7
    from django.contrib import admin
    from django.urls import path
    from my_firstapp.views import add_nums_view
    urlpatterns = [
    path('add/', add_nums_view, name='add'), """include the view created earlier"""
         path('admin/', admin.site.urls),
    ]

    pic 14

    Take note of the add/ – that’s the name of the page you’ll be calling.

    Finally, under the template folder, create a new file called add and save it as html file.

    Within your html file (add.html), just use the ginger tag to render out the dictionary you created earlier in your views.py like this:

    1
    The product of the operation from the view is:{{added}}

    To see the output of your newly created app, open up the browser and navigate to the add view as described below:

    output

    Conclusion

    We’ve been able to create a simple application which can perform a simple mathematical operation using Django. This is just an introductory article to Django. The framework offers a lot of functionalities such as forms handling, database migration and a lot more.

  • Author
    Posts
Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.
Log In

Primary Sidebar

FORUM   MEMBERSHIP

Log In
Register Lost Password

POPULAR   FORUM   TOPICS

  • How to find the title of a song without knowing the lyrics
  • Welcome Message
  • How To Change Or Remove The WordPress Login Error Message
  • The Art of Exploratory Data Analysis (Part 1)
  • Getting Started with SQL: A Beginners Guide to Databases
  • Replacing The Default SQLite Database With PostgreSQL In Django
  • 100% Disk Usage In Windows 10 Task Manager – How To Fix
  • Forums
  • About
  • Contact

© 2021   ·   Ehi Kioya   ·   All Rights Reserved
Privacy Policy