How to setup django project with virtual environment

How to setup django project with virtual environment

By Nilesh Darji
2min read

What is Django?

Django is a robust web framework for Python that allows developers to build dynamic and robust web applications quickly. In this tutorial, we will review the steps to set up a Django project from scratch.

Before we start, make sure you have Python and pip (Python package manager) installed on your machine.

  • Install virtualenv using pip:

What is the virtual environment?

Virtual environments are used to isolate specific Python environments on a single machine, which is useful for managing dependencies for different projects. To create a virtual environment, run the following command:

pip install virtualenv
  • Now, create your project folder.
mkdir project
cd project
  • Create a virtual environment for your project:
virtualenv env

This will create a new directory called "env" within your project directory, which will contain the Python executable and all the packages you install for this project.

  • Activate the virtual environment:

    • for Powershell

        env\Scripts\activate.ps1
      
    • for Command Prompt

        env/Scripts/activate.bat
      
    • for Linux

        source env/Scripts/activate
      

You should see the name of your virtual environment in parentheses at the beginning of the command prompt, indicating that it is active.

(env) user@machine:~$
  • Install Django using pip:
pip install django
  • Create a new Django project:
django-admin startproject myproject

This will create a new Django project called "myproject" in the current directory.

  • Run the Django development server:

This will start the development server at http://127.0.0.1:8000/. You can access the Django administration site at http://127.0.0.1:8000/admin/.

cd myproject
python manage.py runserver

Congratulations! 🎉🎉 You have successfully set up a new Django project. From here, you can start building your web application and take advantage of the many features that Django provides.

I hope this tutorial has helped you get started with Django. If you have any questions or need further assistance, please don't hesitate to ask.

Share on :

Subscribe to my newsletter

Read articles directly inside your inbox. Subscribe to the newsletter, and don't miss out.

© 2024 Nilesh.