Django Framework
Django is an open source framework used to develop website and works on the web server.
What is a framework?
A framework is a particular set of rules, ideas which you use in order to deal with problems or to decide what to do.
Coming to Django:
- It is basically a high level python web application framework that enables the rapid development of web applications.
- It is an open source python framework.
- It follows the MVT (Model view template) architectural pattern.
Advantages of Django:
- It is fast and simple.
- It is secure.
- It suits any web app.
- It is well established.
- MVT support and Object-Oriented approach.
- Built- in authentication and authorization
- Packaging system.
Example of Django web applications:
- Youtube
- Instagarm
- Spotify
- Dropbox
The MVT (Model view template) architectural approach:
- The MVT is a software design pattern.
- Model- It is going to act as the interface of your data.
- View- The view is the user interface – what you see in your browser when you render a website.
- Template – A template consists of static parts of the desired HTML outputs as some special syntax describing how dynamic content will be inserted.
MVT Flowchart:
Installation of Django::
- Install python from python.org and run the installation.
- Click on Add python to path in the installation wizard and also click on customize installation and select all the options and click on install.
- Now to check if the python has been installed, open cmd and type command python. This will show the installed python version.
- Now to check packages installed in python, run the command pip freeze.
- To install django, run the command pip install django.
- Now, to check if django has been installed, run command pip freeze. It will show the packages installed.
Creating a new Project in django:
- Open cmd and run the command django-admin. This will show all the commands in django-admin.
- Now to create a new project in django, we will first change the directory to the new folder in which you want to create the new project.
- Run the command, django-admin startproject projectname. This will create a new project folder.
Running a development server:
- Open cmd and change the current directory to the django project folder using cd command.
- Now, run the development server (project) using the command python manage.py runserver. This will start the server on port 8000.
- You can check the server by visiting the url provided by the cmd on running the command i.e localhost:8000.
- To stop the server we can use Ctrl+C.
Project Building through VSCode::
Installing VSCode:
- Download Visual studio code setup from their official website.
- Now run the setup file and install.
- Open VSCode and install extensions for python, django, django templates, html css support.
- Now click on File > open folder to open your django project folder.
- Now click on Terminal > new terminal.
- We can run our commands in this terminal instead of cmd.
- Change the directory to your project directory using cd command.
- Now run the command, python manage.py runserver to start the development server.
Folder structure of django:
- Open file explorer and open your project folder.
- In the first project folder, create new folders and name them templates, static, media.
- Templates folder will store all the HTML files from your project.
- Static folder will store all the static files i.e. JavaScript, CSS, JQuery etc.
- Media folder will store all the dynamic img from your project.
Migrating Default Migrations to sqlite3 database:
- Open terminal on your VSCode and change the directory to your project folder.
- Now to migrate the tables, run the command python manage.py migrate.