Django: Everything you need to know


 

Django is a Python web framework that endorses rapid development and clean, pragmatic design. It was created by experienced developers to take care of much of the hassle of web development, allowing you to focus on writing your app instead of reinventing the wheel. It's open source and free.


Why should you choose Django?

  • Ridiculously fast.
  • Reassuringly secure.
  • Exceedingly scalable.
  • Fully loaded.
  • Incredibly versatile.

GETTING STARTED

DISCLAIMAR:  This tutorial is running on windows so some of the commands may differ.

Before we begin, ensure that python is installed. Next is to install a virtual environment.  This comes in handy if you plan to run multiple programs, it will prevent your programs from colliding.


Run "pip install virtualenv"


Once its installed you can create a virtual environment in the directory.

Run "virtualenv environment_name"

In this case my environment  is venv so if I want to activate it, I'd run:


You can install Django by running the "pip install django" command



STARTING YOUR FIRST PROJECT

In django, the django-admin command is used to run most of all the needed functions in django, an example is creating a new project.


This would create a folder that has some files in it. Lets go through this folder.
 note:  blog in the command above is the name of  my project. You can change the name as you wish


A folder was created with two contents:
  • And the manage.py file: This acts as a server unit for your project
  • A sub folder which has a similar name to that of the project

What do all this files mean:

  •  __init__.py : We saw somethin similar to this here . The __init .py files are required for Python to recognize directories that contain the file as packages. This prevents common-named directories, such as string, from unintentionally hiding valid modules that appear later on the module search path.
  • asgi.py: ASGI (Asynchronous Server Gateway Interface) provides an interface between async Python web servers and applications while it supports all the features provided by WSGI.
  • settings.py: It is a required file in all Django projects. It contains all of the configuration values required for your web app to function properly, such as database settings, logging configuration, where to find static files, API keys if you work with external APIs, and a variety of other things.
  • urls.py: This links to different directories in the project
  • wsgi.py: The main use of deploying with WSGI is the application callable which the application server uses to communicate with your code.
Now that we understand the project base, we need to create the app inside the project directory.

How do we create an app?

I have created an app called 'core' which created a new folder:

  • migrations directory: holds all the changes to the database.
  • admin.py: pushes models to the admin dashboard.
  • apps.py: This file is created to help the user include any application configuration for the app.
  • models.py: this allows you create data models for your app.
  • tests.py: It is a type of debugging function. Allows you run backend test to ensure the app runs properly.
  • views.py: this is where you work on the display for individually pages.

DJANGO PROJECTS
Django is a bit more complex so the projects would be linked with videos. Ensure to attempt it first.

Blog Archive