DEV Community

What are the codes to run in order, for a Django project? I have python3 and pip installed

Bret on August 17, 2020

What are the codes in order to run?
So first I create a ENV?
Then Django?

Collapse
 
rehmatworks profile image
Rehmat Alam • Edited

Apart from a dockerized solution discussed by Robert, the simplest way to get started is to create a virtual environment on your local machine to install the packages needed by your Django application. Here are the steps that I follow whenever I need to work on a new project.

Step 1: Virtual Environment

I have created a venv directory in my home directory and I create virtual environments there. I create a new virtual environment for each of my projects to keep things separated and clean.

virtualenv -p python3 ~/venv/newproject

Step 2: Create a Project

Navigate to the directory where you want to create your project and then execute this command:

django-admin startproject newproject

Step 3: Activate Virtual Environment

Activate the virtual environment, and install Django in your virtual environment.

source ~/venv/newproject/bin/activate
pip install Django

That's it. Now you should install all the requirements of your project keeping the virtual environment activated and thus you will be able to freeze your requirements to requirements.txt cleanly. Moreover, you will be able to keep the rest of your main Python environment clean on your local machine.

I hope this helps. This is what I follow as a practice while building Django projects. And when it comes to deploying the project to the server, I freeze the project's requirements to a requirements.txt file from the virtual environment that I created just for that project.

Collapse
 
yobretyo profile image
Bret

thank you!!
this is what I've figured out so far.... is this good as well?:
just by entering/running....

1) python3 -m venv customname-env

2) source customname-env/bin/activate

3) python -m pip install Django

Collapse
 
rehmatworks profile image
Rehmat Alam

Yep, this is exactly what I've mentioned above. You are on the right track :)

Thread Thread
 
yobretyo profile image
Bret

but, I need a ENV first?

Thread Thread
 
rehmatworks profile image
Rehmat Alam • Edited

You can create the virtual environment before creating your project or after creating the project. To create the project, you use django-admin command and I assume that django-admin command is available globally on your machine. Once the project is created, you should create a virtual environment for it and then you should install all the requirements of the project in that virtual environment (including the main Django package).

So the virtual environment's role here is just to allow you a dedicated place to install your project's requirements. This way, you don't mess your system with the packages required by a certain project.

p.s. if django-admin command is not available globally in your terminal, first create the virtual, activate it, install Django package, and then create your project.

Collapse
 
robert197 profile image
Robert

Install docker and run django and the db inside docker containers. Use docker compose for composition.
There are bunch of examples of dockerized environments for Django project at GitHub and so on. Just google for it.
Have fun hope it helps.

Collapse
 
daveparr profile image
Dave Parr • Edited

I'd recommend having a look at @williams.vincent here, and his books on Django. I think they're great.

Collapse
 
sa2908 profile image
Sanket Agarwal

django-admin startproject
Then you are good to go

Collapse
 
yobretyo profile image
Bret

What about.....
python -m pip install Django?

Collapse
 
sa2908 profile image
Sanket Agarwal

To create a env run this command
virtualenv

Collapse
 
sa2908 profile image
Sanket Agarwal

I believe you have already activated your virtual environment and then you have installed django in it. If not then activate it then run the following command
pip install django

Collapse
 
yobretyo profile image
Bret

I want to use the environment