A Beginner's Guide to Django in Web Development
If you are just starting your journey into the world of web development, you might have come across the term "Django" a very versatile and multipurpose Python programming language framework, most especially for web development. Let us check and know how Django makes our web development journey enjoyable and easier.
Introduction:
Django is an open-source, popular and easy-to-use framework because of its large community. Django with a lot of cool features helps to fasten your web development because it requires no or less configuration.
HERE ARE THE IMPORTANCE OF DJANGO:
Rich and Large Ecosystems or communities.
Admin panel by default.
Lots of Libraries.
Good for SEO.
ORM - object-relational mapper for database interactions.
To install Django on your computer, make sure you have Python already installed otherwise, download and install it.
To start a Django project, create a folder/directory you want to store your web app on your desktop, from your terminal using this command.
mkdir "folderName"
Then enter the directory.
cd "folderName"
In Django, it is best practice to work in the virtual environment which helps to isolate project dependencies and modules from the system-wide Python installation. To install a virtual environment (venv) use this command.
python -m venv "nameofvirtualenvironment"
python3 -m venv "nameofvirtualenvironment"
once your virtual environment is created, you have to activate it to work within it. To activate it, use this command :
# On Mac :source nameofenv/bin/activate
# On Windows: nameofenv\Scripts\activate
Then install on your terminal using any of these commands:
pip install django
pip3 install django
Once installed, to check the version you are using, run this command.
django --version
A project is the base folder of all your apps, i.e. The entire application or the name of your website. To start your web development project with Django, run.
django-admin startproject "projectName"
This command will create an environment like this, duplicating the project name. The first project name is the project base name to contain all your apps, while the duplicate is the project package containing the URLs and Configurations.
projectName/
manage.py
projectName/ # package
__init__.py # indication of package
settings.py # module 1
urls.py # module 2
wsgi.py # module 3
Creation of an app: An app refers to a submodule of the project. they are little components that make up your project. It's self-sufficient and not intertwined with the other apps in the project. you can have many apps in a single project or one app in many projects. i.e. An e-commerce project or website will contain many apps like cart, product, customers and orders. to create an app, use this command:
python manage.py startapp "appName"
This is what the outcome will look like:
projectName/ # website name
manage.py
appName/ # app 1
__pycache__ # indication of a package
migrations # module
__init__.py # indication of a package
admin.py #module
apps.py #module
test.py #module
views.py #module
urls.py #linking urls to base urls module
projectName/
To view your project in the browser, run this command. This command starts Django’s built-in development server on the local machine with IP address 127.0.0.1 and port 8000.
python manage.py runserver
By default, it's port 8000 though you can specify the port you want to run the server. After that, your website is fully ready for content to be added to it.
why use Django?
Django follows Don't repeat yourself rule "DRY" i.e. code is re-usable which helps in time management due to its Architecture. Taking a closer look at Django Architecture, Django is Written in pure Python with a clean Pythonic structure. It started as a Model–View–Controller (MVC) framework architecture which allows developers to change the visual part of an app and the logic part separately, without affecting one another. However, developers usually refer to Django’s architecture as Model–View–Template (MVT) (Model, View, and Template).
A model contains the essential fields and behaviors of data you’re storing which maps to a single database table. Django officially supports four databases: PostgreSQL, MySQL, SQLite, and Oracle. Information about your data, which makes data manipulation easy (create, read, update, and delete objects (Records) in the original database are contained in models. Models communication between layers is possible only via an application programming interface (API).
The view executes three tasks: It accepts HTTP requests, applies logic provided by Python classes and methods, and provides HTTP responses to clients’ requests. In other words, the view fetches data from a model and either gives each template access to specify data to be displayed after processing.
def home(request):
# performs logic
return HttpResponse("Hello world from Django!")
The templates Django has a powerful template engine and its markup language. Templates are files with HTML codes that are used to render data. The contents of these files can be static or dynamic. There is no logic in a template other than representing data.
%load% "static"
%include% "dynamic"
Conclusion:
Django is an amazing, powerful tool and explicit in simplifying the web app development journey. As a beginner, understanding Django can improve your work and foster productivity. Therefore, let's jump in and build together.
Subscribe to my newsletter
Read articles from Obuesi Ogonna directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by