DEV Community

Cover image for Come along for 20 days of deep Django learning experience with me
Sarah Nyambura Kiiru
Sarah Nyambura Kiiru

Posted on

Come along for 20 days of deep Django learning experience with me

Day 1: How I understood and practiced about the structure of Django


The first thing is to understand what a structure is.

A stucture

is the organized way in which parts of something are arranged or built.It helps one to understand where things belong and maintain one's project as it grows helping in collaboration without confusion.

Django project structure

To be able to have a project structured in the Django style you run the following command to start the project.

django-admin startproject <project_name>
Enter fullscreen mode Exit fullscreen mode

This is how the project structure will look like afterwards:

my_project/
    manage.py 
    my_project/
Enter fullscreen mode Exit fullscreen mode

manage.py - It is a command-line utility used to runserver, migrations etc
my_project (directory with the same name as your project) - This directory contains the project-wide settings and configurations. The files it contained are as below:

my_project/
    __init__.py
    settings.py
    urls.py
    asgi.py

Enter fullscreen mode Exit fullscreen mode

init.py - This empty file tells Python to treat the directory as a package. It's necessary for importing files across different modules something you'll do a lot in Django projects.
settings.py - Contains all the configuration settings for your Django project, such as installed apps, middleware, database settings, static file paths, and more.
urls.py - Acts as the "table of contents" for your site. It defines how URLs are routed to views — basically deciding what happens when someone visits a specific page.
asgi.py - Entry point for ASGI (Asynchronous Server Gateway Interface), which allows your Django app to support asynchronous features like WebSockets and background tasks.
wsgi.py - Entry point for WSGI (Web Server Gateway Interface), which helps traditional web servers like Gunicorn or uWSGI serve your Django project. This is what powers your site in most production environments.
start project
Something to note is to ensure you have created a virtual environment in VS code so as to start the django project
You need to run the server so that to make sure the project runs(a rocket like thing will be displayed in the browser to confirm that)

python manage.py runserver
Enter fullscreen mode Exit fullscreen mode

server run

In order to get to practice on the django structure I created two applications for my day1 learning of django: a journal and an about apps
To be able to create the apps I used:

python manage.py startapp journal
python manage.py startapp about 
Enter fullscreen mode Exit fullscreen mode

Each app contains files and a folder which are:
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.

Then I registered the 2 apps in the settings.py file

register

apps

For the 2 apps I created a folder templates for each.

For the about app the template folder contained an about folder that has an about.html file

about
For the journal app the template folder contained an journal folder that has an entries.html file

journal

in the settings.py had to tell Django where to find the template

{'DIRS': [BASE_DIR / "templates"],}
Enter fullscreen mode Exit fullscreen mode

template

Then routed the URL so that the templates to be visible in the browser
I did this by creating urls.pyfile for each app and linking it from view.py file of each app
For journal app view.py

app1

For about app view.py

Image description

For journal app urls.py

url1
For about app urls.py

url2

For The whole project URL file urls.py

wholeurl

I then started the developer server
and used this link for me to get results
http://127.0.0.1:8000/journal/diary_entries/

one


http://127.0.0.1:8000/about/about_me/

three


My project about creating a diary was complete I had some challenges but got through but did not stop me from proceeding.
This diary apps enabled me to get to understand how the Django structure works.

Top comments (0)