Creating a programming environment
After selecting a folder in my text editor (VS Code), I opened the terminal to run
python -m venv env
to create an environment called env
.
.env/Scripts/activate
or
Source env/Scripts/activate
are commands used to activate the programming environment
Installing Django
I used
pip install django
in the newly created environment to install Django
Project Directory
To start my project directory, I run:
django-admin startproject myproject
in the vs code terminal to generate root directory with project name, which in this case is called myproject
this project is blue in color as shown in the figure above
Project Setting Directory
Inside the 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 in the figure below:
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!
settings.py
: Contains all the project’s settings and configurations.
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
Applications
Django projects are composed of one or more apps. Each app is a Python package that follows a certain convention. In this case I created two apps called app1 and app2 respectively
This is how I created the applications inside an already initiated Django project
manage.py startapp app1
for app1 and the picture bellow shows how it looks a text editor, which in this case is VS Code
manage.py startapp app2
for app2 and the picture below shows how it looks like in VS Code
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.
Templates
Templates(what users see, HTML and CSS) are typically stored in a templates
directory within each app or in a project-wide templates directory.
The selected file that is blue in color is the template for the first app
The selected file that is blue in color is the template for the second app
This stracture helps organize Django Project making it easier to maintain, especially for begginers.
Top comments (0)