DEV Community

"edisthgirb"[::-1]
"edisthgirb"[::-1]

Posted on

Terms Explained: Django

This article deals with explaining terms related to Django in a short and easy way.
I haven't spent much time writing this, so obviously this isn't the most perfectly formatted thing but still readable enough.

Terms Covered in this article:

  1. Django
  2. Framework
  3. Creating a Django Project
  4. Apps
  5. Views
  6. Models
  7. Migrations
  8. Creating Superuser
  9. Templates
  10. URL dispatcher
  11. Forms
  12. QuerySets
  13. Working with Django ORM via shell

Okay, let's start

Django

  • Django is a Web Framework for perfectionists with deadlines.
  • It is used to develop fast, scalable, secure websites.
  • Popular Websites that are built with Django are Instagram, Spotify, Youtube, Washington Post, and so on.

Framework:

  • A Framework is a library of reusable modules (building blocks).
  • A framework defines a structure for our applications.

Creating a Django Project:

Creating a book management project:

django-admin startproject bookdb .
python manage.py runserver

Apps:

  • Django project can contain multiple apps.
  • Apps are functional areas in our Django project. We divide the project into small functional areas for example for a shop, order management, product management, etc.
  • We can reuse them across different Django projects.
  • For example, Creating a books app: python manage.py startapp books

Views:

  • A view function is basically a function that gets called by Django when the user navigates to a particular page.
  • For example, from the homepage, when we go to /books, our browser sends an HTTP request to our web server. Django takes that request, inspects the URL, and figures out that we want to see the list of books and it will call a view function and the job of this function is to return the response to the browser/client. So this function should generate some HTML markup to be returned to the client. Our browser will get this HTML content and display it as a page.

Models:

  • A model is a representation of a real-world concept.
  • For example, in a book management system, we can have models like Book, User, Review, and so on.

Migrations:

When we make a new model or modify an existing one:

python manage.py makemigrations
python manage.py migrate

Creating superuser:

We have to create a superuser to access the admin panel.

Creating superuser:

python manage.py createsuperuser

And then we enter the username, email and password.

Templates:

  • Using template tag {% %} in Django we can write dynamic code in Django templates, for example, loops or if statements.
  • Whenever we use double curly braces syntax {{ }} in a Django template, Django evaluates the expression that we put there and render it in HTML.

URL dispatcher:

  • A clean URL scheme is important in an important detail in a high-quality web application.
  • Django lets us design URLs however we want, with no framework limitations.

Django Forms:

  • With forms, we have absolute power over our interface.
  • We can either define one from scratch or create a ModelForm which will save the result of the form to the model.
  • Like every important part of Django, forms have their own file: forms.py

QuerySets:

  • A QuerySet is, in essence, a list of objects of a given Model.
  • QuerySets allow you to read the data from the database, filter it, and order it.

Working with Django ORM via shell:

Examples for a book app:

Importing the Model:

from books.models import Book

Getting All Objects:

Book.objects.all()

Adding a New Object:

Book.objects.create(name = "Sample Book", author = "Sample author")

Getting a specific object by field:

book = Book.objects.get(id = 1)

Editing a field of the above object:

get_book.author = "Author was edited"

Saving the edited object:

get_book.save()

Deleting the above object:

get_book.delete()

Thank you, that's it for this article.

Top comments (0)