Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It helps you build secure and maintainable websites quickly, so you can focus on writing your app without needing to reinvent the wheel. 🛠️
- Fast Development: Get your web application up and running quickly! ⏩
- Secure: Protects against common security threats by default. 🔒
- Scalable: Handles high traffic and large amounts of data gracefully. 📈
- Versatile: Suitable for all types of projects, from simple to complex. 🧩
- Admin Interface: Automatically generated admin panel to manage your site.
- ORM: Object-Relational Mapping to interact with databases easily.
- Templating Engine: For dynamic HTML generation.
- Form Handling: Simplifies form rendering and validation.
- Authentication: User authentication system ready to go.
Websites that use Django:
- Instagram 📸
- Pinterest 📌
- Mozilla 🦊
- Khalti 💰
- National Geographic 🌍
Django follows the MVT (Model-View-Template) architectural pattern:
- Model: Defines the data structure; it's the database layer. 🗄️
- View: Contains the logic that interacts with the model and renders a response. 🧠
- Template: Deals with the presentation layer; how data is displayed to the user. 🎨
- DRY (Don't Repeat Yourself): Encourages reusability and clean code. ♻️
- Convention over Configuration: Makes decisions for you, so you can focus on your app. 📋
- Pluggable Apps: Easy to integrate third-party apps or create reusable ones. 🔌
While Django is powerful, it's good to know other frameworks:
- Flask 🧪
- Lightweight and minimalistic.
- Requires more configuration.
- Laravel 🧙♂️
- PHP framework with elegant syntax.
- Ruby on Rails 💎
- Based on Ruby, focuses on convention over configuration.
- Python 🐍
- Make sure Python is installed on your system.
- Virtual Environment 🌐
- Isolates your project dependencies.
- VS Code 💻
- A powerful code editor.
Optional:
- Chrome Extension for Django 🧩
- Enhances your development experience.
virtualenv
pip install virtualenv
To create virutal env
virtualenv myenv
Activate using bash
source myenv/Scripts/activate
pip install Django
- Vs Code
Optional
- Chrome Extension For Django
Go to virtual env and run this command.
django-admin startproject myweekroutine
python manage.py runserver
python manage.py runserver 8181
manage.py - A utility for interacting with Django project from command.
settings.py - Setting files for your project. urls.py - Define URL routing for project. init.py - Marks this directory as python package. asgi.py Configuration for ASGI [Asynchronous Server Gateway Interface]
- wsgi.py - Configuration for WSGI [Web Server GateWay Interface] -- db.sqlite - Default SQlite database
Django app is web application that does one thing. For e.g. blog, ecommerce, forum, etc.
- Django project can contain multiple apps.
- Apps are reusable
python manage.py startapp <appname>
python manage.py startapp myapp
-
Project is overall configuration that ties multiple apps together.
-
App is independent module/feature of project.
Define URL routing for project.
- https://technologychannel.org/ - Root URL
- https://technologychannel.org/about/ - About URL
Logic for handling request and returning response. It can be function or class.
init.py - Marks this directory as python package. admin.py - Register models to apprear on Django admin interface. apps.py - Contain the configuration for app. models.py - Define Database models. tests.py - Used to write unit test for app. views.py - Logic for handling request and returning response. migrations/- Contains migration files for database changes.
- Open settings.py and find INSTALLED_APPS list.
- Add your new app to the list.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp',
]
To define views first you need to import:
from django.http import HttpResponse
def index(request):
return HttpResponse("<h1> Welcome to My Website </h1>")
def contact(request):
return HttpResponse("<h1>Welcome to Contact Page</h1>")
Create file called urls.py
and add urls.
from . import views
from django.urls import path
urlpatterns = [
path('',views.index,name='homepage'),
path('contact',views.contact,name='contactpage'),
]
First go to project's urls.py
and add the code:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('myapp.urls')) # Add this
]
python manage.py runserver
- Create virtualenv
virtualenv myenv
- Activate virtualenv
- On Windows [Bash]
source myenv/Scripts/activate
- On Mac
source myenv/bin/activate
- Install Django
pip install django
- Create Django Project
django-admin startproject myproject
- Open Project on Vs Code and Activate Virtual Env
- Create App
python manage.py startapp myapp
- Register App
INSTALLED_APPS = [
'myapp',
]
- Define Views in App
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("<h1> Welcome to My Website </h1>")
def contact(request):
return HttpResponse("<h1>Welcome to Contact Page</h1>")
- Create
urls.py
file in app directory and create urls.
from . import views
from django.urls import path
urlpatterns = [
path('',views.index,name='homepage'),
path('contact',views.contact,name='contactpage'),
]
- Define App views to project
First go to project's
urls.py
and add the code:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('myapp.urls')) # Add this
]
- Run Project
python manage.py runserver