DEV Community

Chemutai Brenda
Chemutai Brenda

Posted on

DAY 4 OF LEARNING DJANGO.

DJANGO STRUCTURE.

Today i learnt how to do a set for creating a project using Django.

Step 1:
Set up
Creating a programming environment
I open a folder in my installed text editor(VS Code) and runed the following command in the terminal to create an environment called env.:

  python -m venv env
Enter fullscreen mode Exit fullscreen mode

Inside the terminal, I used the following command to activate the environment:

.env/Scripts/activate
Enter fullscreen mode Exit fullscreen mode

STEP 2:
Installing Django

I used the following command in the newly created environment to install Django.

pip install django
Enter fullscreen mode Exit fullscreen mode

STEP 3:
Starting my project.

To start my project i run the following command in the vs code terminal to generate root directory with project name, which in this case is called "MyProject".

django-admin startproject myproject

Enter fullscreen mode Exit fullscreen mode

STEP 4:
The Project Wide Settings.
After creating MyProject root directory, there is another directory called MyProject, just as the project name.
This other directory contains the project-wide settings and configurations as shown below in a diagram:

Image description

init.py:
A file that tells Python that all files in the directory should be considered a Python package.
Without this file, we cannot import files from another directory which we will be doing a lot of in Django and project creation.

settings.py:
Contains all the project’s settings and configurations.
Inside the settings I change the time from 'UTC' to 'Africa, Nairobi'.

urls.py:
The URL declarations for the project are a “table of contents” of your Django-powered site.

asgi.py:
allows for an optional Asynchronous Server Gateway Interface(ASGI) to be run

wsgi.py:
An entry point for Web Server Gateway Interface(WSGI) compatible web servers to serve your project. It helps Django serve our eventual web pages

STEP 5:
Applications.
-Django can be used to create multiple apps where each app is a Python package that follows a certain convention. In this case I created two apps called app1 and app2 respectively.

  • I run the following command from terminal to create app1:
manage.py startapp app1
Enter fullscreen mode Exit fullscreen mode
  • I run the following command from terminal to create app2:
manage.py startapp app2
Enter fullscreen mode Exit fullscreen mode

Image description

  • In the two apps each app came with important files like; > admin.py: Configuration for the Django admin interface.

apps.py:
Configuration for the app itself.

models.py:
Contains the database models for the app.

tests.py:
Contains tests for the app.

views.py:
Contains the request/response logic for the app.

migrations/:
Contains database migrations for the app

STEP 5:
To make Django recognize both apps, I opened mysite/settings.py and added them in INSTALLED_APPS

Image description

STEP 6:
Creating URLS and Writing views:
For app1

  • In app1/views.py i created this code;
from django.shortcuts import render

def app1_home(request):
    return render(request, 'app1_home.html')
Enter fullscreen mode Exit fullscreen mode
  • I then created urls.py for app1 added the following in it
from django.urls import path
from .views import app1_home

urlpatterns = [
    path('', app1_home, name='app1_home'),
]
Enter fullscreen mode Exit fullscreen mode

For app2

  • In app2/views.py:
from django.shortcuts import render

def app2_home(request):
    return render(request, 'app2_home.html')
Enter fullscreen mode Exit fullscreen mode
  • Then I created app2/urls.py:
from django.urls import path
from .views import app2_home

urlpatterns = [
    path('', app2_home, name='app2_home'),
]
Enter fullscreen mode Exit fullscreen mode

STEP 7
Connecting Both Apps in mysite/urls.py
Now it was time to connect both apps to the main URL configuration.

  • In mysite/urls.py I wrote:
from django.contrib import admin
from django.urls import path, include

from django.contrib import admin
from django.urls import path, include

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

Image description

STEP 7:
Adding pages for HTML Pages

  • Templates(what users see, HTML and CSS) are typically stored in a 'templates' directory within each app or in a project-wide templates directory.
    So I created a templates folder inside each app

  • In both app1 and app2, I made this folder structure:

  • For app1:

app1/
└── templates/
    └── app1/
        └── home.html
Enter fullscreen mode Exit fullscreen mode
  • For app2:
app2/
└── templates/
    └── app2/
        └── home.html
Enter fullscreen mode Exit fullscreen mode

STEP 8:
Updating views to render templates

  • In app1/views.py:
from django.shortcuts import render

def home(request):
    return render(request, 'app1/home.html')
Enter fullscreen mode Exit fullscreen mode
  • In app2/views.py:
`from django.shortcuts import render

def home(request):
    return render(request, 'app2/home.html')
Enter fullscreen mode Exit fullscreen mode

With this I was able to learn how to set up a Django structure for creating a project. I also managed to understand how two or more apps can be created inside one project.

Top comments (0)