DEV Community

Cover image for What I Learned While Building the Learning Log
Isah Jacob
Isah Jacob

Posted on

What I Learned While Building the Learning Log

I had the chance to become familiar with essential Django framework concepts while developing the learning log app, and I will go through those concepts in this article.

The Learning Log app is an online journaling application that allows users to record their learning on specific subjects. Users can keep track of the subjects they are interested in and write entries in their journals as they learn more about each one. Users are required to log in or register on the Learning Log home page after the site's description. A user has the ability to add new subjects, edit existing ones, and add new entries.

The Virtual Environment Is Critical 

I install packages and isolate them from all other Python packages in a virtual environment on my computer. This is because it prevents installing Python packages everywhere in the system, which can damage other projects or my system tools, and separates my project libraries from those of other projects.

I discovered that making a folder to store everything is the first thing to do.
I discovered that installing a virtual environment is the first step because this is my first time working with Django.

Python3 -m pip install user virtualenv
Enter fullscreen mode Exit fullscreen mode

Create a virtual environment

I created a virtual environment by running the following command.

python -m venv ll_env
Enter fullscreen mode Exit fullscreen mode

Activate virtual environment

I activated my virtual environment by running the following command

source learning_env/bin/activate
Enter fullscreen mode Exit fullscreen mode

This is specifically for Linux.

Install Django

Once I activate my virtual environment.
I install Django with pip

pip install Django
Enter fullscreen mode Exit fullscreen mode

Creating Project in Django.

I forget the full stop at the end of the command below and I ran into many errors. The full stop prevents folder duplication and makes it easier to deploy your app.

django-admin startproject  learning_logs .
Enter fullscreen mode Exit fullscreen mode

Creating Database

Django uses SQLite to store most of the information for a project in a database.

Viewing Project

python manage.py runserver 
Enter fullscreen mode Exit fullscreen mode

The command above launch the development server and allow me to see how well my app functions on my system. When a URL request comes in, the Django server replies by creating the necessary page and sending it to the browser.

Launching an App

A Django project is structured as a collection of unique apps that interact with one another to function as a whole.

python manage.py startapp learning_logs
Enter fullscreen mode Exit fullscreen mode

This command instructs Django to build the foundation necessary to develop an app.

Setting Up Models

Django is organized on how to handle the data via a model that will be kept in the application. A model is just a code class with logics, features and procedures, like every class you've ever encountered in python.

Activating Models

Grouping apps together in a project helps to keep track of them as the project grows to include more apps. Whenever I want to modify the data that my app manages, I will need to follow these three steps

To activate the models run the command below

python manage.py makemigrations
Enter fullscreen mode Exit fullscreen mode

The command makemigrations tells Django to figure out how to modify the database so it can store the data associated with any new models I defined.

python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

The command above lets Django know that it will prepare the database to store the information needed to handle administrative and authentication tasks.

The Django Admin Site

Django makes it easy to work with your models through the admin site. Only the site’s administrators use the admin site, not general users.

Creating a super user

Django allows you to create a "superuser," a user who has all privileges available on the site. A user’s privileges control the actions that user can take.
Run the following code to create super user and follow the promt.

python manage.py createsuperuser
Enter fullscreen mode Exit fullscreen mode

The Django Shell

The Django shell is the interactive environment, which is excellent for testing and troubleshooting project. I can use the Python interpreter that is launched by the command

python manage.py shell
Enter fullscreen mode Exit fullscreen mode

to browse the data included in the database for my project when it is run in an active virtual environment.

Create Pages

Django has three steps for creating web pages: creating URLs, creating views, and creating templates. Any order would do for these. The structure of a URL is described by a URL pattern. In order for Django to know which page to return, it also instructs it on what to check for when comparing a browser request with a site URL.

The view function retrieves and modifies the information required for that page, mapping each URL to a specific view. A template that contains the page's overall structure is frequently used by the view function to render the page.

Creating URLs

I am made to choose which URLs are required since users access pages by typing URLs into browsers and clicking links. The first URL is the home page's. It serves as the default URL for accessing the project. The fact that the default Django site is currently returned by the base URL, http://localhost:8000/, indicates that the project's setup was successful.

In the url.py in my project folder. I perform the following operations inside this file:

urlpatterns = [
path('admin/', admin.site.urls),
path('', include('app_name.urls')),
]
Enter fullscreen mode Exit fullscreen mode

In my app folder I created urls.py, import path and views and write the following inside.

from django.urls import path
from . import views
app_name = 'name_of_your_app'
urlpatterns = [
# Home page
path('', views.index, name='index'),
]
Enter fullscreen mode Exit fullscreen mode

Composing a View

A view function receives data from a request, prepares it so that it can be used to create a page, and then delivers it back to the browser, frequently using a template that specifies how the page should look.

Open views.py in your app folder and enter the following code:

from django.shortcuts import render
def index(request):
"""The home page"""
return render(request, 'name_of_app/index.html')
Enter fullscreen mode Exit fullscreen mode

The index() function in the views.py file is what Django looks for when a URL request meets the pattern I described. The request object is then passed by Django to this view method. The only code in the method in this scenario is a call to render because we don't need to handle any data for the page.

The original request object and a template that can be used to generate the page are the two arguments passed to the render() function in this case.

Establishing a Template

Every time a page is requested, Django fills in the pertinent information according to the template's description of how the page should look. You have access to all of the view's data through a template.
Make a templates folder inside the app folder. In the

<p>Welcome to Django</p>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)