Web Development Technical Summer School 2019, IIT
Description: Web Development Technical Summer School 2019, IIT Bombay Parth Patil Part 5 Django Dynamic HTML Store only the template and data Create the rendered HTML only when asked for Instead of saving the HTML, send it to the client Allows
Related Topics
Download Presentation
"Web Development Technical Summer School 2019, IIT" is the property of its rightful owner. Permission is granted to download and print the materials on this website for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Presentation Transcript
slide1. Web Development Technical Summer School 2019, IIT Bombay – Parth Patil
Part 5 – Django<br>
slide2. Dynamic HTML Store only the template and data
Create the “rendered” HTML only when asked for
Instead of saving the HTML, send it to the client
Allows changing data (very) frequently
Can recognize user and generate specific content<br>
slide3. Django Web framework,
Written in Python,
Model-view-template architectural pattern.<br>
slide4. Template Format to display the data
Like a sample
HTML (or any other) with variables<br>
slide5. Model Structure to access data
Data access becomes easier
Linked models
OOP – Object Relation Mapping (ORM)<br>
slide6. View Interacts with the user
Performs operations like filling up the Model
Generates the template and returns to the user<br>
slide7. Database (RDBMS) Convenient way to store/access data
Written by top coders
Easy to use APIs
Django – built-in ORM for many databases<br>
slide8. SQLite One RDBMS (Relational Database Management System)
Everything in one file
Easier to manage for smaller applications
Typically slow for larger real-world sites<br>
slide9. Migrations Successively modify database structure
Can go from one point to another easily
Define database with code<br>
slide10. Getting Started $ python -V
$ pip install django
$ django-admin startproject mysite
$ django-admin startapp product<br>
slide11. Getting Started INSTALLED_APPS
python manage.py migrate
python manage.py runserver<br>
slide12. Django Model from django.db import models
class Product(models.Model):
name = models.CharField(max_length=50)
description = models.TextField(blank=True)
image_url = models.URLField(blank=True, null=True) website_url = models.URLField(blank=True, null=True)<br>
slide13. Making/Applying Migrations python manage.py makemigrations
python manage.py migrate<br>
slide14. Django Admin python manage.py createsuperuser
localhost:8000/admin/ #admin.py
from django.contrib import adminfrom product.models import Product
admin.site.register(Product)<br>
slide15. __str__(self) Overriding default method
Useful in admin def __str__(self):
return self.name<br>
slide16. Views # views.pyfrom django.http import HttpResponse
def index(request): return HttpResponse("<h1>Welcome to Django!</h1>")<br>
slide17. URLs # urls.py
from django.contrib import admin
from django.urls import path
import product.views as pv
urlpatterns = [ path('admin/', admin.site.urls),
path('product/', pv.index),
]<br>
slide18. Getting URL Information path('product/<pk>', pv.index),
def index(request, pk):
return HttpResponse("<h1>Welcome to Django! " + pk + "</h1>")<br>
slide19. Querying Data from product.models import Product
def index(request, pk):
p = Product.objects.get(name=pk) return HttpResponse(p.description)<br>
slide20. Rendering a Template from product.models import Product
def index(request, pk):
p = Product.objects.get(name=pk)
context = {'product': p}
return render(request, 'product.html', context)<br>
slide21. Working Template <h1> {{ product.name }} </h1>
<p> {{ product.description }}</p>
<a href="{{product.url}}">URL</a><br>
slide22. Adding more fields Add fields
Make Migrations
Migrate is_discounted = models.BooleanField(default=False)
{% if product.is_discounted %}
<b>Currently under discount</b>{% endif %}<br>
slide23. Extra – storing relational data<br>
slide24. Thank You!<br>
Part 5 – Django<br>
slide2. Dynamic HTML Store only the template and data
Create the “rendered” HTML only when asked for
Instead of saving the HTML, send it to the client
Allows changing data (very) frequently
Can recognize user and generate specific content<br>
slide3. Django Web framework,
Written in Python,
Model-view-template architectural pattern.<br>
slide4. Template Format to display the data
Like a sample
HTML (or any other) with variables<br>
slide5. Model Structure to access data
Data access becomes easier
Linked models
OOP – Object Relation Mapping (ORM)<br>
slide6. View Interacts with the user
Performs operations like filling up the Model
Generates the template and returns to the user<br>
slide7. Database (RDBMS) Convenient way to store/access data
Written by top coders
Easy to use APIs
Django – built-in ORM for many databases<br>
slide8. SQLite One RDBMS (Relational Database Management System)
Everything in one file
Easier to manage for smaller applications
Typically slow for larger real-world sites<br>
slide9. Migrations Successively modify database structure
Can go from one point to another easily
Define database with code<br>
slide10. Getting Started $ python -V
$ pip install django
$ django-admin startproject mysite
$ django-admin startapp product<br>
slide11. Getting Started INSTALLED_APPS
python manage.py migrate
python manage.py runserver<br>
slide12. Django Model from django.db import models
class Product(models.Model):
name = models.CharField(max_length=50)
description = models.TextField(blank=True)
image_url = models.URLField(blank=True, null=True) website_url = models.URLField(blank=True, null=True)<br>
slide13. Making/Applying Migrations python manage.py makemigrations
python manage.py migrate<br>
slide14. Django Admin python manage.py createsuperuser
localhost:8000/admin/ #admin.py
from django.contrib import adminfrom product.models import Product
admin.site.register(Product)<br>
slide15. __str__(self) Overriding default method
Useful in admin def __str__(self):
return self.name<br>
slide16. Views # views.pyfrom django.http import HttpResponse
def index(request): return HttpResponse("<h1>Welcome to Django!</h1>")<br>
slide17. URLs # urls.py
from django.contrib import admin
from django.urls import path
import product.views as pv
urlpatterns = [ path('admin/', admin.site.urls),
path('product/', pv.index),
]<br>
slide18. Getting URL Information path('product/<pk>', pv.index),
def index(request, pk):
return HttpResponse("<h1>Welcome to Django! " + pk + "</h1>")<br>
slide19. Querying Data from product.models import Product
def index(request, pk):
p = Product.objects.get(name=pk) return HttpResponse(p.description)<br>
slide20. Rendering a Template from product.models import Product
def index(request, pk):
p = Product.objects.get(name=pk)
context = {'product': p}
return render(request, 'product.html', context)<br>
slide21. Working Template <h1> {{ product.name }} </h1>
<p> {{ product.description }}</p>
<a href="{{product.url}}">URL</a><br>
slide22. Adding more fields Add fields
Make Migrations
Migrate is_discounted = models.BooleanField(default=False)
{% if product.is_discounted %}
<b>Currently under discount</b>{% endif %}<br>
slide23. Extra – storing relational data<br>
slide24. Thank You!<br>