<< Back to Blog

A Django Project: Setting Up Your First Project and Installing Django

Continuing on in my series of developing A Django Project on using Windows 10 as your operating system– we are going to move forward by creating our virtual environment and installing Django on it.

If you are not familiar with this concept you may find this article helpful for an in-depth explanation on virtual environments, but for now I will try to give a brief explanation: Django has several dependencies it requires in order to run and one Django project’s dependencies will likely be different from another’s. A virtual environment allows you to create a compartmentalized environment for a specific project’s dependencies to live and be maintained. This allows you to run/develop more than one Django project at a time even if they have different dependencies and software versions.

For example, if you were building a new Django project you would create a virtual environment and install the latest version of Django. If at some point you were tasked with modifying an existing Django project you may find that it is running a different software version and different packages altogether. In this case you would deactivate the virtual environment for your brand new Django project and activate one with the correct software versions/necessary packages for the existing Django project you are going to work on without having to drastically change your development environment.

 

Creating the Virtual Environment

  1. Open up Administrator Command Prompt: Click on the search box near the Windows 10 start button or click on the start button and begin typing “cmd” then right-click and choose “Run as Administrator.”
  2. Type ‘mkvirtualenv projectname’.

    This will create and activate your virtual environment with the name “projectname.” You can run this command from any directory as it will create it in a default location. This is determined by the virtualenvwrapper-win package we installed in the previous article. To learn more about virtualenvwrapper-win and change the directory virtual environments are created you can checkout the Github repository for the project. While active, any packages you install will be ‘contained’ to this environment.

  3. In the same Administrator Command Prompt change to the directory you wish to store your Django projects by typing “cd directoryname” or “cd c:\directoryname” depending on your set-up.

    This should not be the same directory as your virtual environment (a good example would be ‘C:\Development\Projects’).

    If you are trying to change directory to another drive in the Windows Command Prompt you would first type: “d:”, and then you would type “cd d:\your-directory\name”

  4. Create the directory for the new Django project: “mkdir projectname”.
  5. Change/move to our project’s new directory: “cd projectname”.
  6. Tell our virtual environment what directory our project lives in: “setprojectdir .” Note: the command includes the period after setprojectdir: “setprojectdir .”

    This command associates whatever project lives in this directory with our currently active virtual environment. Another nice thing about this is when you open up your Administrator Windows Command Prompt and activate your virtual environment you will automatically be moved to the project directory.

 

Installing Django and Starting a Project

  1. From the Administrator Windows Command Prompt, while in your project directory with your virtual environment still active– type: “pip install Django”
  2. While still in your project directory type: “django-admin startproject projectname”

    This will create a new directory within your project directory (it’s alright if it’s the same) and set-up a base Django project.

  3. Change to your new Django project’s install: “cd projectname”.

    This is the directory that contains the file “manage.py”.

  4. Let’s start your Django project’s server: “python manage.py runserver” (some users may need to type “python3 manage.py runserver”).
  5. If all goes well you will see a few messages scroll including “Starting development server at http://127.0.0.1:8000/”– enter than address into your browser to see your new Django install.
  6. After confirming the Django install works you can simply return to the Command Prompt and press “CTRL+C” to cancel/kill the server.

 

Working With Virtual Environments

  1. If you have been following each step your virtual environment should still be active. Let’s go ahead and deactivate it if it isn’t already by typing into our Administrator Windows Command Prompt: “deactivate”.

    If you closed your Windows Command Prompt it will also deactivate the virtual environment.

  2. In any case, if you now wish to work on your Django project that you previously set-up you would simply open an Administrator Windows Command Prompt and type: “workon projectname”.

    This will activate the related virtual environment and take you to that project’s directory.

  3. If you would like to start your Django server again you will need to change directory into the Django project and type the command: “python manage.py runserver” (or for some users “python3 manage.py runserver”).
  4. When you are done working you can press “Ctrl+C” to kill the server and then type “deactivate” to deactivate your virtual environment or simply close the command prompt window.

 

Congratulations! You have your Windows 10 Python/Django development environment configured, you have the ability to install and manage virtual environments to keep your software versions and packages separate and specific to each Django project, and you have a working Django base project up and running.