Introduction
Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.
Why Django?
1. Ridiculously fast.
2. Reassuringly secure.
3. Exceedingly scalable.
Getting started with Django
Before starting Django you need to install django and python to your computer.
step 1 .install python from python.org
step 2. install pip using python apt install python3-pip
step3. install Django
pip install django
Creating your very first Django project
django-admin startproject my_firts_proj
after starting the project you can see several pre installed python files in your project section, further diving to the project folder you will find files like asgi.py,settings.py,urls.py,wisgi.py and at last, you will find the manage.py file
├── my_first_proj
│ ├── __init__.py
| ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
understanding python files
__init__.py
indicates that the files in the folder are part of a Python package. Without this file, we cannot import files from another directory which we will be doing a lot of in Django!asgi.py
allows for an optional Asynchronous Server Gateway Interface to be runsettings.py
controls our Django project’s overall settingsurls.py
tells Django which pages to build in response to a browser or URL requestwsgi.py
stands for Web Server Gateway Interface which helps Django serve our eventual web pages.
The manage.py
file is not part of django_project
but is used to execute various Django commands such as running the local web server or creating a new app.
Running Django application
python manage.py runserver
after running the Django application you will find the development server in your terminal as http://127.0.0.1:8000/
copy the link and paste it into your browser
Starting your first django application
you can start your django application using
python manage.py startapp myapp
after starting your application you have to register your application to your django project
to register your app you have to add your app in installed applications you can find installed application on settings.py file located at your project folder
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# my apps
'myapp',
]
Saying Hello world
In Django, four separate files aligning with this MVT pattern are required to power one single dynamic (aka linked to a database) webpage:
models.py
views.py
template.html
(any HTML file will do)urls.py
However, to create a static webpage (not linked to a database) we can hardcode the data into a view so the model is not needed. That’s what we’ll do here to keep things simple.
The next step is therefore to create our first view. Start by updating the views.py
file in our myapp
app to look as follows:
# myapp/views.py
from django.http import HttpResponse
def homePageView(request):
return HttpResponse("Hello, World!")
Moving along we need to configure our URLs. In your text editor, create a new file called urls.py
within the myapp app . Then update it with the following code:
# myapp/urls.py
from django.urls import path
from .views import homePageView
urlpatterns = [
path("", homePageView, name="home"),
]
On the top line we import path
from Django to power our URL pattern and on the next line we import our views. By referring to the views.py
file as .views
we are telling Django to look within the current directory for a views.py
file and import the view homePageView
from there.
Our URL pattern has three parts:
- a Python regular expression for the empty string
""
- a reference to the view called
homePageView
- an optional named URL pattern called
"home"
The last step is to update our my_first_projs/urls.py
file. It’s common to have multiple apps within a single Django project, like myapps here, and they each need their own dedicated URL path.
# django_project/urls.py
from django.contrib import admin
from django.urls import path, include # new
urlpatterns = [
path("admin/", admin.site.urls),
path("", include("myapp.urls")), # new
We’ve imported include
on the second line next to path
and then created a new URL pattern for our pages
app. Now whenever a user visits the homepage, they will first be routed to the pages
app and then to the homePageView
view set in the myapp/urls.py
file.
This need for two separate urls.py
files is often confusing to beginners. Think of the top-level my_first_proj/urls.py
as the gateway to various url patterns distinct to each app.
We have all the code we need now. To confirm everything works as expected, restart our Django server:
# Windows
python manage.py runserver
# macOS /linux
python3 manage.py runserver
If you refresh the browser for http://127.0.0.1:8000/
it now displays the text “Hello, World!”
Sai ho?
Just wow need more