Open In App

Django Basics

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Django is a Python-based web framework which allows you to quickly create web application without all of the installation or dependency problems that you normally will find with other frameworks. When you’re building a website, you always need a similar set of components: a way to handle user authentication (signing up, signing in, signing out), a management panel for your website, forms, a way to upload files, etc. Django gives you ready-made components to use. django-basics

Why Django?

  • Django is a rapid web development framework that can be used to develop fully fleshed web applications in a short period of time.
  • It’s very easy to switch database in Django framework.
  • It has built-in admin interface which makes easy to work with it.
  • Django is fully functional framework that requires nothing else.
  • It has thousands of additional packages available.
  • It is very scalable. For more visit When to Use Django? Comparison with other Development Stacks ?

Django architecture

Django is based on MVT (Model-View-Template) architecture. MVT is a software design pattern for developing a web application. MVT Structure has the following three parts – Model: Model is going to act as the interface of your data. It is responsible for maintaining data. It is the logical data structure behind the entire application and is represented by a database (generally relational databases such as MySql, Postgres). View: The View is the user interface — what you see in your browser when you render a website. It is represented by HTML/CSS/Javascript and Jinja files. Template: A template consists of static parts of the desired HTML output as well as some special syntax describing how dynamic content will be inserted. To check more about Django’s architecture, visit Django Project MVT Structure

Installation of Django

  • Install python3 if not installed in your system ( according to configuration of your system and OS) from here . Try to download the latest version of python it’s python3.6.4 this time.
  • Install pip- Open command prompt and enter following command-
  • Install virtual environment- Enter following command in cmd-
  • Set Virtual environment- Setting up the virtual environment will allow you to edit the dependency which generally your system wouldn’t allow. Follow these steps to set up a virtual environment-
    1. Create a virtual environment by giving this command in cmd-
python -m virtualenv env_site
  1. Change directory to env_site by this command-
cd env_site
  1. Go to Script directory inside env_site and activate virtual environment-
cd Scripts
activate
  • Install Django- Install django by giving following command-
pip install django
  • To initiate a project of Django on Your PC, open Terminal and Enter the following command
django-admin startproject projectName
  • A New Folder with name projectName will be created. To enter in the project using terminal enter command
cd projectName
  • To create a basic app in your Django project you need to go to directory containing manage.py and from there enter the command :
python manage.py startapp projectApp
  • Now you can see your directory structure as under :
  • To consider the app in your project you need to specify your project name in INSTALLED_APPS list as follows in settings.py: 

Python3




# Application definition
 
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'projectApp'
]


  • So, we have finally created an app but to render the app using urls we need to include the app in our main project so that urls redirected to that app can be rendered. Let us explore it. Move to projectName-> projectName -> urls.py and add below code in the header
from django.urls import include 
  • Now in the list of URL patterns, you need to specify app name for including your app urls. Here is the code for it – 

Python3




from django.contrib import admin
from django.urls import path, include
 
urlpatterns = [
    path('admin/', admin.site.urls),
    # Enter the app name in following syntax for this to work
    path('', include("projectApp.urls")),
]


  • Now You can use the default MVT model to create URLs, models, views, etc. in your app and they will be automatically included in your main project.


Last Updated : 18 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads