Building with Django: Brick by Brick

Kerem Can ÇELEPKOLU
Django Unleashed
Published in
5 min readFeb 3, 2024
“Microsoft Designer”

Building Django Application

Hey, today we are going to learn how to build a Django application and talk about what we should pay attention to.

The first thing that we need to clarify before we pass to building the Django application is the “virtual environment”.

Virtual Environment

You can say what is the virtual environment or you may have heard it for the first time. Don’t worry, I will explain it briefly. You can pass this but I strongly recommend you that don’t do that because starting with the best possible setup will save you from a lot of problems in the future!

So let’s set up a virtual environment. Virtualenv keeps it separate for each project and isolates it. This means that changes you make to one website will not be reflected in other things you develop. Magnificent, isn’t it?

Virtual Environment Setup

To create a folder in any location, you can right-click on the desired location and select “New Folder” from the context menu. Then, you can rename the folder to your preferred name.

To open the folder in the VSCode editor, you can either drag and drop the folder into the VSCode window or use the “Open Folder” option from the File menu.

To open the terminal in VSCode, you can use the shortcut Ctrl + ` (backtick) or go to the View menu and select “Terminal”. This will open the integrated terminal at the bottom of the VSCode window.

Then, run this command :

python -m venv <Venv name>

It will create a folder called your Venv name, in my case I have set “MediumDjango” and here it should look like this:

Working with a Virtual Environment

Now, wake up! This is the most important part of the blog.

Sometimes harder to remember when working in a virtual environment, because you should be aware that you are working in the virtual environment you have just created.

If you are working without venv, your terminal will look like this:

In another case (with venv) will look like this:

In the second case, your venv name will appear left-hand side as you see in the above figure. Let’s explain how to activate the virtual environment before passing the installation of Django and other stuff.

Activate Virtual Environment

In Windows:

<your venv name>\Scripts\activate

In Linux or Mac:

source <Your venv name>/bin/activate

Yes, using a virtual environment is indeed a valuable setup in many careers, especially in the field of software development. It allows you to create an isolated environment with its own set of dependencies, libraries, and configurations, separate from your system’s default setup. This helps ensure that your project runs smoothly and consistently, regardless of the underlying system.

Once activated, you can install project-specific dependencies, run your code, and perform other tasks within the virtual environment. It’s important to remember to activate the virtual environment each time you work on your project to ensure that you are using the correct set of dependencies and configurations.

Installation of Django

There are two main ways to install Django.

  • By using requirements.txt file
  • By manuel installaiton

By using requirements.txt file

Make sure that your pip version is updated by using this command:

python.exe -m pip install --upgrade pip

By using this icon create a file called requirements.txt. You can specify a different name, but requirements.txt is the usual name, as all developers know. As soon as you have done this, open that file.

MediumDjango
└───MediumDjango(it is your venv)
└───requirements.txt

Write this line: Django==5.0.1

Here I write 5.0.1 but you can download and set any version of Django according to your need by changing the just only version part.

Now open your terminal and ensure that you are in your virtual environment. Run this command:

pip install -r requirements.txt

It will automatically download Django to your project.

Here is the additional information about requirements.txt, While you are developing your project, you will download some other packages or some other libraries, you need to keep them somewhere(in our case it is requirements.txt) with its versions because if the other developers or your co-workers want to use your project, they will run above command and setup easily.

I will show you how can you save the information that I explained. For example, you downloaded the OpenAI library in your project. Here is the command for saving dependencies for that library into requirements.txt:


pip freeze > requiremets.txt
Usage of requirements.txt

By manuel installaiton

You can shortly run this command if you want to install by manually:

pip install Django

When you have done the whole thing, you can set Django in your project, and in the next step, you are ready to starting a project by running this command:

django-admin startproject config .

In my case, I give a name config but you can give as you want. Please be careful about dot because the dot’s duty is that it will start your Django project into your config file. After running the command your project file structure will look like this :

File structure after starting the project

Don’t worry about the new things such as manage.py, urls.py, setting.py. We will cover those things and more upcoming blogs.

In this journey of building a Django application, we’ve covered essential concepts and practical steps to create a robust web application. Remember, the key to mastering Django lies in consistent practice and experimentation. As you embark on your projects, don’t hesitate to explore new features, seek community support, and continuously enhance your skills.

Stay tuned for upcoming blogs where we’ll delve into more topics, tips, and tricks related to Django development. Whether it’s optimizing performance, integrating third-party libraries, or exploring Django’s powerful ecosystem, we’ll continue this learning journey together. Happy coding, and may your Django applications thrive in functionality and elegance!

--

--