Search

Making a Django project

This is for the documents leaving history for studying Django referred to the book called “Jump to Django
 Disclaimer
This post is a personal study note created while learning the topic discussed. Some information may be inaccurate or incomplete. If you notice any errors or have suggestions, I’d sincerely appreciate your feedback.
In Django, a "project" typically represents an entire website or web application. A project can contain one or more "apps", each responsible for a specific feature or functionality, such as user login, blog posts, or admin pages.
Each app is a self-contained module that can be reused across different projects if designed properly. For example, you might have an app that manages blog posts and another that handles user authentication.
Django also provides several built-in apps under the django.contrib package, such as the authentication system (auth) and the admin interface (admin). Developers can also create their own custom apps and include them in the project.
Together, these apps make up the complete Django project that is, the website itself.
Common misunderstanding
In Django, the term "app" is different from what we usually mean by a mobile app like those on Android or iOS.
While a mobile app is typically a complete, standalone program, a Django app is more like a reusable module that handles a specific feature or functionality within a larger project.
For example, you might have separate Django apps for blog posts, a message board, or a chat system, each focused on a particular task.
These apps are then combined within a Django project to form a complete website.

Creating a Django project

Since you may have multiple Django projects, it's essential to create a root directory to organize them. In my case, I will use the c/github/django/projects directory as the root directory for all my Django projects.
(You can create the root directory named projects at any location you prefer.)
Create the root directory at c/github/django/projects and navigate to it. Then, enter the mysite virtual environment as shown below.
source /c/venvs/mysite/Scripts/activate
Shell
복사
We will be creating our Django project inside the projects folder.
Although the virtual environment we created last time (mysite) is located outside this folder, we will activate it from within the projects directory and use it to manage our project.
Note
When activating a Python virtual environment, the exact command you need to use depends on the terminal (or shell) you're working in. For example, if you're using the Windows Command Prompt (cmd), you can simply run
C:\venvs\mysite\Scripts\activate.bat
Shell
복사
Just executing the .bat file will activate the environment, and you'll see something like (mysite) appear in front of your prompt. In PowerShell, you need to run the .ps1 script instead, and sometimes adjust your execution policy settings.
However, in Unix-style terminals like Git Bash, macOS Terminal, or Linux, things work differently. If you run the script directly, it may execute in a new subshell that closes immediately, so the environment settings won't stay active in your current shell. That’s why, in these environments, you must use the source command like this.
source /c/venvs/mysite/Scripts/activate
Shell
복사
The source command tells the shell to apply the changes directly to your current session, so things like environment variables and the prompt update correctly.
(For now, i am currently using Git Bash.)
When the virtual environment is activated, you’ll see the environment name, like (mysite) appear in your terminal prompt.
In some terminals, a line break may cause (mysite) to appear above the prompt, but it’s still part of the prompt itself. This is completely normal and indicates that the virtual environment is active.
If you’ve followed along so far, you're now ready to create your Django project. First, we'll create a new directory named mysite inside the root projects directory, and then generate the Django project within that folder.
mkdir mysite
Bash
복사
You can create a directory named mysite using the mkdir command, which stands for make directory. After that, you can use the ls command(if you are using Windows CMD, it will be dir command), short for list, to view the folders and files in your current location. If everything went well, you should see that the mysite directory has been successfully created.
cd mysite/
Bash
복사
After that, use the cd command, which stands for change directory, to move into the directory you just created with the mkdir command.
Then, create the Django project by running the command django-admin startproject config . in the terminal.
django-admin startproject config .
Bash
복사
At this point, pay attention to the period (.) after config. The dot represents the current directory. This means that the project will be created using the current directory, which in this case is mysite, as the base folder.
Once the command is executed, Django will generate several directories and files inside the mysite folder that are necessary for running a Django project. We will take a closer look at those files and folders later.
You can also create a Django project without manually creating the mysite directory beforehand. Many tutorials follow this approach
django-admin startproject mysite
Shell
복사
However, if you create the project this way, Django will generate a folder named mysite, and inside it, another folder with the same name resulting in a structure like mysite/mysite.
This duplication can make project management more confusing, as both the outer and inner directories share the same name.
For this reason, this post recommends using a mysite/config structure instead, which keeps the directory names more distinct and easier to manage.
If the project was created successfully, you should see several new files and folders, including the manage.py file. The presence of manage.py, along with the configuration folder (such as config), indicates that your Django project has been set up properly.

Running the development server and accessing the website

Now that the mysite project has been created, it's time to run it.
Enter the following command python manage.py runserver
python manage.py runserver
Bash
복사
The message "You have 18 unapplied migration(s)." means that the initial setup for Django’s default database configuration has not been completed yet. You can ignore this for now. For the moment, just focus on the middle line shown in the output.
Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
Bash
복사
It means that the Django server has started at http://127.0.0.1:8000/, and that you can stop the server by pressing CONTROL-C.
If you go to the address http://127.0.0.1:8000/, then you will see the screen below
You can enter http://localhost:8000/ instead of http://127.0.0.1:8000/ and get the same result. This is because 127.0.0.1 is a local IP address, and localhost is a domain name that points to it. Both refer to your own computer (the local machine).
However, unfortunately, other people won’t be able to access your website just yet.
This is because the Django development server is currently running only on your local environment, and external users cannot reach it through localhost or 127.0.0.1.
To make your site accessible to others, you would need a public IP address or domain name, or use a special tool like ngrok to expose your local server to the internet. For now, let’s focus on building a great website.

Quickly entering the virtual environment

To enter the mysite virtual environment, you normally have to launch the command prompt, navigate to the c:/github/django/projects/mysite directory, and then run the c:/venvs/mysite/Scripts/activate command.
To save time and avoid repeating these steps every time, let's create a batch script that performs the entire process at once.
Create a file named mysite.cmd using a text editor like Notepad.
(You can replace the paths with the locations where you created your own project and virtual environment.)
@echo off cd c:/github/django/projects/mysite c:/venvs/mysite/scripts/activate
Shell
복사
The reason it's called mysite.cmd is simply because it's based on the name of the virtual environment. Feel free to choose any name you like for the file.
(A file with the .cmd extension is called a batch file. You can think of it as a file that allows you to enter and execute commands all at once.)
[file name : C:/venvs/mysite.cmd]
The content of the batch file instructs the system to move to the C:/github/django/projects/mysite directory and then execute the C:/venvs/mysite/Scripts/activate command.
For Git Bash users only, not applicable to Windows CMD!
(You can skip this part if you are using Windows CMD)
If you're using Git Bash, a .cmd file like the one above may not run directly. Instead, it's better to create a dedicated .sh script that works with Git Bash.
#!/bin/bash # Move to the Django project directory cd /c/github/django/projects/mysite || exit # Activate the virtual environment source /c/venvs/mysite/Scripts/activate
Shell
복사
Now, move the .sh file you created earlier into your current project folder.
and let’s go to the folder where your project is
cd /c/github/django/projects/mystie
Shell
복사
Then, let's grant execution permission to the file (you only need to do this once).
chmod +x mysite.sh
Shell
복사
And when you run source mysite.sh, it will immediately activate the virtual environment.
source mysite.sh
Shell
복사
Then, you'll see that you've successfully entered the mysite virtual environment.
Now, let's go back to how to set environment variables for users who are using the Windows CMD.
If you want this batch file to be executable from anywhere in the command prompt, you need to add the C:/venvs directory to the system’s PATH environment variable.
First, press <Windows + R> on your keyboard, type sysdm.cpl into the Run dialog, and then click OK.
This will open the System Properties window. From there, select the Advanced tab and click the Environment Variables button.
This will open the Environment Variables window. Under User variables, select Path and then click the Edit button.
This will open the Edit Environment Variable window. Click the New button.
Then, as shown in the next image, add the C:\venvs directory and click the OK button.
Finally, in the Environment Variables window, click the OK button.
This adds the C:\venvs directory to the system PATH, allowing you to run the mysite.cmd command from anywhere in the command prompt. Restart the command prompt to make sure the updated PATH is applied.
Then, run the set path command to check the current PATH environment variable. You should see that C:\venvs has been successfully added.
Then, no matter what directory you're currently in, you can simply type mysite to activate the virtual environment for that project, like this.