Python

Tag: django

Django

Django is the most popular web framework for Python.

Why Django?

Django organizes your website into apps.

It has an template engine, database support, an ORM, automatic admin interface, URL routing and way more.

Python and Django Full Stack Web Developer Bootcamp

Apps

Every app is a component of a website, these apps can be reused in other websites. An app can be anything from a poll, blog to a forum.

In the long run that will save you a lot of development time.
It’s also easy to add apps from other projects. Apps are pluggable.

Each app has its own views and models.

  • Models: store the apps data. If you want a dynamic website (using database), you need models!
  • Vies: python functions that return the webpages users see. They often use models to retrieve the data.

Inside the project directory, each app has its own directory.

Note: In the models, an object relational mapper (ORM) can be used. An ORM maps Python objects to the MySQL database, meaning you don’t have to write SQL queries.

Template engine

Django has a template language out of the box. Inside HTML, tags can be used.
If you want to print a variable:

{{ post.title }}

You can loop through a list in a template:

{% for post in forum.posts.all %}
...
{% endfor %}

Even if statements can be used in a template:

{% if admin %}
...
{% else %}
...
{% endif %}

You may like:

Admin Interface

Django comes with an admin interface!

It’s tedious work to create admin sites for clients to edit database data.
Django automates creation of admin interfaces for models.

django admin interface

Forms

Even forms come with Django. Automatic input validation, generate forms, convert data to python datatypes

Django admin

Django comes with an automatic admin interface.

We just created a (database) model in the previous article. Now we can create a backend management tool for it.

This is one of the most powerful features of Django.

Related course:
Python and Django Full Stack Web Developer Bootcamp

Admin interface

You can open the admin interface with a web browser, but to open the admin interface, first we need to change a file. Open the file movies/admin.py
This is the file contents:

from django.contrib import admin

# Register your models here.

Lets add some code:

from django.contrib import admin
from .models import Movie

admin.site.register(Movie)

Start the Django server:

python manage.py runserver

Open the url: http://127.0.0.1:8000/admin/

Admin login

A login screen will popup. Let’s enter the matrix!

django admin

Django admin add user

Create a superuser.

python manage.py createsuperuser

Type a username and password. You should see this message:

Superuser created successfully.

Now open the webpage again and login.

The admin screen

If you see the screen below, good job!

django site admin

The model ‘Movie’ is there!

Django admin add

If you click on the model, a new screen shows up:

django add to model

We can add new movies here. If you click on the button, all the fields are there to add a new movie, exactly as we specified it in the model.

django insert into database

How cool is that?
All the CRUD operations work.

Note: CRUD is short for create, read, update and delete. Common database operations

We can now interact with the complete database system from this graphical interface.