Dive into Django Web Development

web development n.w
1 / 24
Embed
Share

Uncover the intricacies of Django, a powerful web framework written in Python that follows the model-view-template architectural pattern. Learn about dynamic HTML, template formatting, data modeling, database management, and more in this comprehensive exploration of web development at the IIT Bombay's Technical Summer School 2019.

  • Django
  • Web Development
  • Python
  • IIT Bombay
  • Technical

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Web Development Technical Summer School 2019, IIT Bombay Parth Patil Part 5 Django

  2. 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

  3. Django Web framework, Written in Python, Model-view-template architectural pattern.

  4. Template Format to display the data Like a sample HTML (or any other) with variables

  5. Model Structure to access data Data access becomes easier Linked models OOP Object Relation Mapping (ORM)

  6. View Interacts with the user Performs operations like filling up the Model Generates the template and returns to the user

  7. Database (RDBMS) Convenient way to store/access data Written by top coders Easy to use APIs Django built-in ORM for many databases

  8. SQLite One RDBMS (Relational Database Management System) Everything in one file Easier to manage for smaller applications Typically slow for larger real-world sites

  9. Migrations Successively modify database structure Can go from one point to another easily Define database with code

  10. Getting Started $ python -V $ pip install django $ django-admin startproject mysite $ django-admin startapp product

  11. Getting Started INSTALLED_APPS python manage.py migrate python manage.py runserver

  12. Django Model from django.db import models classProduct(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)

  13. Making/Applying Migrations python manage.py makemigrations python manage.py migrate

  14. Django Admin python manage.py createsuperuser localhost:8000/admin/ #admin.py from django.contrib import admin from product.models import Product admin.site.register(Product)

  15. __str__(self) Overriding default method Useful in admin def __str__(self): return self.name

  16. Views # views.py from django.http import HttpResponse def index(request): return HttpResponse("<h1>Welcome to Django!</h1>")

  17. 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), ]

  18. Getting URL Information path('product/<pk>', pv.index), def index(request, pk): return HttpResponse("<h1>Welcome to Django! " + pk + "</h1>")

  19. Querying Data from product.models import Product def index(request, pk): p = Product.objects.get(name=pk) return HttpResponse(p.description)

  20. 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)

  21. Working Template <h1> {{ product.name }} </h1> <p> {{ product.description }}</p> <a href="{{product.url}}">URL</a>

  22. Adding more fields Add fields Make Migrations Migrate is_discounted = models.BooleanField(default=False) {% if product.is_discounted %} <b>Currently under discount</b> {% endif %}

  23. Extra storing relational data

  24. Thank You!

More Related Content