0x0017 - Django Getting Started

0x0017 - Django Getting Started

Django is a web framework based upon the Python language.  Large professional sites such as Instagram, Spotify, Youtube use Django.

Programmers prefer using Python because it means faster development - especially when you are using stepping debuggers such as Pycharm. Not only that but Pycharm Professional series come with Django support natively built-in.

sudo apt install python3 python3-pip
sudo pip3 install django

For this introductionary tutorial - we are working in Pycharm Professional 2022.2.4.  We create a new project..

The entire skeletal framework is built for us:

If we were to do this from the command line we would use:

django-admin startproject <project>

We are working from the base tutorial. What we might cover could be redundant from it - but it reinforces the understanding of it's structure. Going over the standard structure..

  • mysite/ is a container for the project.
  • manage.py Django project manager.
  • mysite/mysite/ is the Python package
  • mysite/__init__.py An empty file that tells Python that this directory should be considered a Python package.
  • mysite/settings.py Settings for the project.
  • mysite/urls.py URL mappings for the project
  • mysite/asgi.py entry point fro ASGI-compatible web servers.
  • mysite/wsgi.py entry point fro WSGI-compatible web servers.

To run the server:

python manage.py runserver

We can see it has stood up a listener at 127.0.0.1:8000 - what is there?

A full web presentation front is there for us.

We can set how/what ports it listens on:

python3 manage.py runserver 0.0.0.0:8000
# to open any port below 1024 requires root
sudo python3 manage.py runserver 0.0.0.0:80
  • Management of your Django application is done through the manage.py manager and this is built-in to Pycharm Pro:

Even autocomplete is built-in! - It shows us 'startapp'  Doing this from the command line would simply be:

We can see it has added a folder and a structure inside it:

We edit views to add our first view:

Now we need to map the view to a URL, we add url.py in the same directory structure:

This app url now needs integration into the master URL file:

Interestingly django has it's own auto-debugging page updates - currently the guide is not working.

We found our error - a simple / missing:

Changed to:

Next we go over the settings.py file and then we do a migrate.

python3 manage.py migrate

Our application addition to the settings.py is a little different as the directory name of the project is different:

  • Note: 'django_learn_a' matched the project module 'django_learn_a'

By the guide we define our data model:

And then activate it with:

python3 manage.py makemigrations polls

Understanding that:

Now we will create the superuser:

And we re-run the server to access the admin:

The administration is built-in:

Finally we register our question class in the admin.py

The above actually did not work, we had to be explicit in the path importation:

A complete manager for the data object is already done where we have added the Question() class:

With it our console is is logging the accesses:

Linux Rocks Every Day