Mustapha7018
Posted on March 15, 2023
PART 1 - Creating a Django Project
Django is a robust web framework for building server-side applications in Python, providing developers with a wealth of tools and features to streamline their workflow. In this article, I'll walk you through the simplest and most comprehensive way to start your Django project with ease. Whether you're a seasoned developer or just getting started, this step-by-step guide will have you up and running in no time, so you can focus on building the web app of your dreams.
Create a virtual environment
A virtual environment is a self-contained directory that contains a Python installation and any required dependencies for your project. Just like you wouldn't want your project's materials to get mixed up with the rest of the house's stuff, you don't want your project's Python packages to get mixed up with the global Python installation or other projects' packages on your computer. With a virtual environment, you can keep your project's packages in a safe and isolated space, so you can work on your project without worrying about causing conflicts or breaking other things. This is how we install and create virtual environment:
Open a terminal or command prompt and navigate to the directory where you want to create your project. Run the command below to install virtual environment:
<user\project-directory> pip install virtualenv
Next, we create our virtual environment by running the command:
<user\<project-directory> py -m virtualenv <env-name>
The virtualenv we installed in step 1 is a Python module that helped us create our virtual environment in step 2.Once you finish creating your virtual environment, you'll see a directory created within your main project directory, bearing the name you specified during your creation of virtual environment in step 2.When you open your virtual environment directory, you'll see these files and subdirectories below:
Activate your virtual environment
To activate your virtual environment, navigate to the Script directory and run this command:
<project-directory\<env-name\Scripts>activate
The activate command enables you to activate your environment workspace. In the terminal, you will see your environment name in parenthesis indicating that your environment is active.
(env-name)<project-directory\<env-name\Scripts>
NOTE: To deactivate your virtual environment, you can simply run deactivate. Your environment name in parenthesis won't be seen along with your working path anymore.
Install Django and Pin your Dependencies
After successfully creating your virtual environment, we are now going to look at how to install Django in your virtual environment and keep track of all the dependencies you'll install and use while building your project. To do so, follow these steps:
Run pip install django
in your script directory. This installs the current version of Django available at that particular time. As I said in the beginning of the article, we do not want to mix up dependencies of different projects and settings at the global level. In all, you should see something like this:
After the Django installation, navigate back into your main project directory and run the following commands to pin your dependencies:
(env-name)<project-directory>py -m pip freeze > requirements.txt
Once finished, check your main project directory, you will find a text file which contains the dependencies you have installed so far. By including a requirements.txt file in your project, you can recreate the exact same environment on another system or at a later time. This helps ensure that your project works as expected, regardless of the environment it is running in. Moreover, sharing the requirements.txt file with other developers makes it easier for them to set up their development environment with the same package versions as yours.
Start a Django Project
This section is the most exciting part after you are successfully done with creating your virtual environment and freezing your dependencies. A Django project provides a structure and set of tools for building web applications quickly and efficiently. In Django, a project is typically composed of one or more applications, which are reusable components that can be shared across different projects. Each application in a Django project is designed to perform a specific function, such as handling user authentication or managing a database.
To create a Django project, run:
(env-name)<project-directory>django-admin startproject <project-name>
The above command creates a directory with the project name you specified. Inside that folder, you will find a manage.py file and a subfolder bearing the same name as the project you just created. The manage.py file is very crucial and it is a command-line utility that enables you to perform variety of tasks related to Django projects, such as creating database tables, running development servers, and executing tests. This is the folder structure so far after running this command:
project-name/
manage.py
project-name/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
Moreover, the subfolder bearing the same name as the project name contains Python files that for managing your project. What the files basically does is summarized below:
init.py: In Python, the init file is used to mark a directory as a Python package. When Python sees this file in a directory, it knows that the directory should be treated as a package, and that the code inside can be imported as a module.
settings.py:This file contains all the settings and configurations for the Django project, such as the database configuration, installed apps, middleware, and more. Developers can modify this file to customize the behavior of their Django project.
urls.py:This file contains the URL patterns for the Django project. It maps the requested URLs to the appropriate views in the project. Each app within the project can have its own urls.py file to define its own URL patterns.
asgi.py & wsgi.py: Both ASGI and WSGI are interfaces that allow web servers to communicate with Python web applications. The main difference is that ASGI allows for more flexibility in terms of handling different types of requests and supporting asynchronous operations.
NOTE: You have to be in the directory where manage.py is located before running any command-line utility. Else you you'll run into serious errors.
Run your Django Project
Finally, your project has been created. You can view it in your local host by running the command:
(env-name)<project-directory\project-name>python manage.py runserver
This runserver command is a built-in command that is included in Django to start your development server. In your terminal, you will see what is shown in the image below. Hold down ctrl and click on the local host address circled with blue to view your project in your browser. Forget about the message in red!
Any changes you make to your code will be automatically reloaded by the server, so you can test your changes in real-time. This is how you will see the default project template running in your browser:
Summary
Overall, these are the basic things you need to know about getting started with Django projects. My next article will talk about how to install and configure apps in your Django Project. These are the excerpts on how to start your Django project:
- Run
pip install virtualenv
to install virtual environment. - Run
py -m virtualenv <env-name>
to create your virtual environment in your working directory. - Navigate into your Script directory and run activate to activate your virtual environment. To deactivate, run deactivate.
- Run
pip install django
to install Django in your script directory. - Navigate back to your main project directory and run
py -m pip freeze > requirements.txt
to freeze/pin your dependencies. - Run
django-admin startproject <project-name>
to create your project. - Navigate your newly created project directory where your manage.py is and run
python manage.py runserver
to start your development server. - Hold down ctrl and click on the address in your terminal to lead you to your project running in your browser!
Always remember to have your virtual environment activated throughout your development process and run the commands from your terminal.
Follow me and give this article a like if you found it useful!! Your comments are also welcomed :-)
Posted on March 15, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.