Python (Django) Classroom Series – 09/Sept/2021

Using the Pattern Matching for Filtering Operations

  • For filtered results we can also look up whether the parameter contains part of the string we are looking for
from books.models import Book, Contributor, BookContributor, Publisher
books = Book.objects.filter(title__contains='Who')

Preview

Retrieving Objects by Excluding

  • If we want to fetch the objects that donot match certain condition we can use exclude Preview

Retrieving Objects using the order_by()

Preview

Using the update()

  • Change the first_name of contributor who has last_name ‘BLANCHARD’ to ‘DR KENNETH’ Preview

Using the delete()

  • Create a new publisher and delte it Preview

Views

  • Django views can be broadly classified into two types
    • function-based views
    • class-based views

Function-Based Views

  • Function based views are implemented as python functions.
def home(request):
    return HttpResponse('Welcome to Django application')
  • The view function defined here name home takes a request object as an argument an return Http Resonse object having th welcome usage.
  • The advantage of using function base views is that they are readable for other programmers
  • The major disadvantage of function-based views is that the code cannot be re-used and mad as concise as the class based views for generic use case

Class-Based Views

  • As the name implies, class based views as implemented as Python classes.
  • Using the principles of class inheritance, the classes are implemented as sub class of Django’s generic view classes.
  • Django generic view classes come with various pre-built properties and methods that can provide shortcuts to writing clean, reusable views.
  • In most cases developer often need to render HTML page without needing any data inserted from databasem or any customization specific to the user, in this case it possible to simple inherit Django’s TemplateView and specify the path of the html file
from django.views.generic import TemplateView

class HomePage(TemplateView):
    template_name = 'home_page.html'

  • However a disadvantage of class-based view is that the code is often less readable for someone new to Django

Developer Setup

  • MySQL Setup on your local system
  • Design a html form (no need of ids)
    • Publisher
    • Contributor

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin