About Django - koglak/SWE573 GitHub Wiki

Django Admin

Django has automatic admin interface. It reads the data from models and provide an interface accordingly to manage content of website. However, it is not possible to build entire website by using admin page.

Django Models

Defination 1:

Django web applications access and manage data through Python objects referred to as models. For More Information!

Defination 2:

A model is a class that represents table or collection in our DB, and where every attribute of the class is a field of the table or collection. Every model inherits from django.db.models.Model. For More Information!

Defination 3:

In Django, the messy issues with SQL is a solved problem: you don’t have to use SQL at all unless you want to. Instead, you use a Django model to access the database. In Django, the model is the object mapped to the database. When you create a model, Django executes SQL to create a corresponding table in the database. For More Information!

image

Django ORM and QuerySets

The object-relational mapper (ORM) in Django makes it easy for developers to be productive without prior working knowledge of databases and SQL. QuerySets represent a collection of objects from the database and can be constructed, filtered, sliced, or generally passed around without actually hitting the database. No database activity occurs until we do something to evaluate the QuerySet.

A queryset is simply a list of objects from the Django Models. It can be used to query data as Create, Filter, Update, Order, etc. Queryset can be written in Django Shell or in views.py.

Django Views

At their simplest they are a Python function or class that takes a web request and return a web response. Views are used to do things like fetch objects from the database, modify those objects if needed, render forms, return HTML, and much more.

A view function, or “view” for short, is simply a Python function that takes a web request and returns a web response. This response can be the HTML contents of a Web page, or a redirect, or a 404 error, or an XML document, or an image, etc. Example: You use view to create web pages, note that you need to associate a view to a URL to see it as a web page.

Run Django's interactive console in terminal:

 python manage.py shell