Django Installation Guide - Visual Studio 2022 [Date: 1st June 2022]

[Note:] This guide was necessitated because using the "Create Python Django Project" option in Visual Studio created a lot of troubles. That's because it created the Django Project and Django app with some dummy names. And then changing the names as per need would never let you run the website. I faced the same issue with using the PyCharm's Django project facility. So I needed such a guide.


1. Create a Python Web Project.











2. Configure your new project as follows:










3. Now, in Solution Explorer, right-click on Python Environments and click Add Environment.











4. Add the new environment with the following configuration:














5. Now, ensure that this newly created virtual environment is activated. For example, in the following screenshot, the Activate Environment option is grayed-out.



















With this, when you check the project properties, in the Toolbar above the Project Properties, Visual Studio will display the current Python Interpreter as the one present in this newly created virtual environment in this sub-directory: .\venv\Scripts\.

6. Now, open a Developer Powershell by doing View -> Terminal.
There, activate the virtual environment by running the command .\venv\Scripts\activate




Now, you have the virtual environment's interpreter activated in the project properties as well as in ther Terminal Window. 

7. Now, you can install Django package either using the Terminal, or in the Visual Studio UI's Python Environment window.
  • Steps to install python packages using the Python Environment Window are listed here. This method is preferred as it ensures that the Solution Explorer is updated.
  • Otherwise, you can proceed with Django package installation using the Terminal with the activated virtual environment (as per step-6 above), following the django documentation here:
    • https://docs.djangoproject.com/en/4.0/topics/install/#installing-official-release
    • https://docs.djangoproject.com/en/4.0/intro/install/

8. Now, you can also open the PyCharm's equivalent of 'Python Console' by opening an Interactive Window for your virtual environment's Python interpreter. Do this:











This would open PyCharm's equivalent of a 'Python Console' as follows:



You can verify that this Interactive Window is using the Virtual Environment's python interpreter instead of using the global python interpreter by following the instructions in writeup on Python interpreter configuration checking.
[Note:] What Visual Studio calls Interactive Window or what PyCharm calls 'Python Console' is actually the Python REPL Prompt. Read this for more on the Visual Studio Interactive Window/REPL Prompt.
[REPL = read-evaluate-print-loop]

9. In order to make your django website accessible from other computers on the network, django requires you to use the command python manage.py runserver 0.0.0.0:8000
With this, you'll be able to access the django website from other computers on the network using the URL http://ComputerIP:8000
The equivalent of python manage.py runserver 0.0.0.0:8000 in Visual Studio is to: 
  1. go to Project Properties (not Solution Properties) 
  2. go to the Debug tab 
  3. add :8000 or :8000/admin to the launch URL 
  4. add 0.0.0.0: to the Run Server Command Arguments.


Comments