To Start A Django Project In Virtual Environment

To Start A Django Project In Virtual Environment

A Basic Refreshment Information

Hey, Today I want to share information about how to start a django project. I know this topic is more detailed but I want this writing a shortcut.

1. For Windows

Package can be installed by this command:

pip install venv

The venv package should be downloaded and the virtual environment provided by it should be used. To create this virtual environment

python -m venv <virtualEnvName>

must be entered.

After this is entered, a folder named "virtualEnvName" will be created and some scripts will be found in this folder. According to the editor you use, you need to activate the relevant virtual environment by running the scripts in it. For Powershell

. <virtualEnvName>\Scripts\Activate.ps1

activates the virtual environment with. To deactivate

deactivate

command can be executed.

To do the same with WSL python and pip must be downloaded. After that virtual environment modules must be downloaded.

pip install virtualenv
sudo apt install python3.8-venv

To create a virtual environment

python -m venv <virtualEnvName>

Then to activate the virtual environment

source .<virtualEnvName>/bin/activate

must be entered. To deactivate

deactivate

In this virtual environment, the desired libraries can be downloaded and this does not affect the operation of other packages. If a virtual environment is going to be used, the next steps must be performed while the virtual environment is active.

3. Installing Django

Django can be download by pip

pip install django

To check the package that was downloaded afterward

pip list

This will output a list of packages. If you see django here, it is ok. As an alternative you can check with

python -m django --version

Here, if we see that the django package has been downloaded, we can proceed to the next steps and start our project.

4. Creating A Django Project

Your root project will be started with the command below. This project name should not contain words like django or test.

django-admin startproject <projectName> .

With the command below, you can start your development server (port 8000 default) and see your project in the browser.

cd <projectName>
python manage.py runserver

5. Creating An Application Under Django Project

There may be more than one application under the project. To create an application

python3 manage.py startapp <applicationName>

With the command, we will create an application named <applicationName>.

6. Registering Your Application To Django Project

In the projectName/projectName/settings.py file add your application name to other applications.

INSTALLED_APPS = [
    ...,
    'applicationName',
    # 3rd party

    # django
    'django.contrib.admin',
    'django.contrib.auth',
    ...
]

7. Result

As I said at the beginning of this essay, this is a brief answer for how to start a Django project. For detailed information please check the docs.

Thank you for reading.