Apidays New York 2024 - The value of a flexible API Management solution for O...
Introduction to Django
1. Introduction to Django
Presented by: Ahmed Salama
Software Engineer at Ibtikar Technologies
A web development framework for Python
2. ● What is Django?
● History
● MVC and MVT design patterns
● Django Architecture
TOC
● Django Modules
● Install Django
● Steps to create Django project
● Let’s make a small ToDo App
3. What does “Django” means?
● Django is named after Django Reinhardt, a jazz manouche guitarist from
the 1930s to early 1950s. To this day, he’s considered one of the best
guitarists of all time.
● Django is pronounced JANG-oh. Rhymes with FANG-oh. The “D” is silent.
Django is a free open source Web frameworks written in python
1
2
3
4. History
● Created in 2003, When the Web programmers at Lawrence Journal-World
newspaper, Adrian Holovaty and Simon Willison, began using Python to build
applications.
● It was released publicly in 2008, Official site: https://www.djangoproject.com/
● Django’s now run by an international team of volunteers.
● Which sites use Django?? DjangoSites.org contains list of django websites and you
can register yours.
● Well-known sites: Disqus - BitBucket - Instagram - Mozilla Firefox(help page, Add-
ons ) - Pinterest( 33 million visit per month) - NASA - Onion(satirical articles) - The
washington post - eventbrite
5. Model-View-Controller(MVC) Design pattern
● Model: A representation of your data. It’s not the actual data, but an interface to the data. It allows you to pull data
from your database without knowing the intricacies of the underlying database(ORM).
● View: What you see in the browser. It’s the presentation layer for your model.
● Controller: controls the flow of information between the model and the view. It uses programmed logic to decide
what information is pulled from the database via the model and what information is passed to the view.
6. Model-View-Template(MTV) Design pattern
● Model: Data access layer.
● Template: The presentation layer. (Takes the role of ‘View’ on MVC)
● View: the business logic layer. Controls what a user sees. This layer contains the logic that accesses the model and
defers to the appropriate template(s). You can think of it as the bridge between models and templates.
● The controller role is handled by the Framework itself and this is the most advantage of Django’s MVC.
7. Django Modules
★ Administration interface (CRUD interface)
★ Authentication system
★ Comments system
★ Forms handling
★ Sessions
★ Syndication framework (RSS and Atom Feeds)
★ Caching
★ Internationalization
★ Localization
★ Custom Middleware
8. Why Django for Web development?
● Provides auto generated web admin to ease the website administration.
● Provides you template system to define HTML template for your web pages to
avoid code duplication.
● Allows you to define what URL be for a given Function.
● Everything is in python.
● Django is not only one of many web frameworks available. It is one of the
leading frameworks for developing scalable, secure and maintainable web
applications.
10. Steps to create Django project
➢ Start a project $ django-admin startproject <project_name>
➢ Start an application $ python manage.py startapp <app_name>
➢ Define your models # models.py
➢ Write your templates templates/
➢ Define your views # views.py
➢ Create URL mapping # urls.py
➢ Test Application # test.py
➢ Edit the Django’s default database(SQLite) to MySQL or Postgresql -if you want.
➢ Define DB Settings in Settings.py
➢ Deploy Application -make it public(Heroku, etc.)
11. Django’s generated code (Starter code)
> MyProject/
> MyProject/
> __init__.py -> Call wsgi.py
> settings.py -> Configuration file
> urls.py -> URL Mapping for the project
> wsgi.py -> The start point of the project
> MyApp/
> __init__.py
> migrations/ -> History(Versions) of database changes
> admin.py -> Register your models here(add them to admin website)
> apps.py
> tests.py
> models.py
> views.py
> manage.py -> Interact with django( runserver, migrate(commit changes to database)
..)
12. Add to the starter code..
● Import application’s view.py in urls.py. < from myApp import views as app1_views>
Append functions to urls < path('home/', app1_views.index, name='index'), >
● Add your application to intalled_apps array which in settings.py > to add it to admin site.
● Templates/ directory contains the html files of each application. < os.path.join(BASE_DIR, ‘templates’) >
● Staticfiles/ directory to add css, js, img files to it. <STATICFILES_DIRS = [ os.path.join(BASE_DIR, "staticfiles"), ] >
● Create the Admin website > $python3 manage.py migrate
● Create super user( system admin) > $ python3 manage.py createsuperuser
● Design Error pages (400, 403, 404, 500) -Django take care of it in local development.
● Adjust the settings file for production(deployment)
Run Django’s server with $ python3 manage.py runserver
13. Let’s make a small project!
ToDo Application
1- TaskModel has: Name, Description, Owner(foreignKey), Responsibles(Many2one), is_done.
2- Color it with ‘staticfiles’.
3- add/remove tasks from admin site.
14. Further reading & Resources
● Django Documentations: https://docs.djangoproject.com/en/2.1/
● Udemy course: https://www.udemy.com/python-ecommerce-build-a-django-ecommerce-web-application/
● The Django Book: https://djangobook.com/
● Django Girls: https://djangogirls.org/ ( We inspire women to fall in love with programming.)