Introduction
Python is a very powerful and popular programming language which is used for Machine Learning, AI, Data Science. But if you are thinking of using this Python to create a web application, which can use all the features of Python then Yes, we can create a web application using Python with the help of Django. Basically, Django is an open source and Python-based web framework for creating the web application. Now, Django is available with Visual Studio as well. If you are using Visual Studio 2015/2017 or higher version, then you can find that different kinds of Python application templates are available in Visual Studio. Django Web Project template is one of them. This article will demonstrate to you how to create the first Django Python application in Visual Studio. So, let's start the practical demonstration and see how to create a Django Python Web Application in Visual Studio.
If you would like to learn Python with Visual Studio and want to start from the beginning, then follow these articles:
Create a Project
Open Visual Studio 2017 or higher version and click to File > New > Project or simply press Ctrl+Shift+N. Here, you will get New Project windows which have three panels, just choose Python > Web from the left panel and choose Django Web Project from the middle panel as you can see in the following image. Give the appropriate project name and solution name, both can be different or similar as well. If you would like to change the project saving location, you can change it from Location and finally click on OK.
Once you click on the above OK button, it will start creating a Django Python project for you. Next screen will ask you to set up the virtual environment for your Django Web Application. We can create this virtual environment in three different ways:
- Using "Install into a virtual environment", you can create a virtual environment and install all required latest packages like Django framework into this virtual environment directly.
- Using "Install into Python 3.7", you can directly install all the required packages into Python globally, so that it will be available for all the next project if you create under Python 3.7.
- Using "I will install them myself", here you can leave the setting the virtual environment and packages. You can do it later once your project will be ready.
For this demonstration, we will choose the first option as "Install into a virtual environment". It will help us and install all required packages under a virtual environment so that we can isolate this virtual environment from other environments.
Once you will choose the first option, the next screen will ask you to project the name for your virtual environment and select the base interpreter for your virtual environment. Here, we will choose Python 3.7 as a base interpreter. Don't forget to check the checkbox for "Download and install packages" and click to Create button. It creates a virtual environment with the same name as you have provided and install all required packages into this.
The following image shows the installation of required packages for a virtual environment like PYTZ, DJANGO, etc. Apart from this, it will create a "requirements.txt" file inside your solution which is nothing but a configuration file for the latest Django framework.
Once it will be done with virtual environment creation and installation of required packages, the project will be ready. Here is our first Django Python Web application. You can see the project structure with the following image. We have Python Environment, where we will find all the installed packages and if required, we can add many more.
Add Virtual Environment
Sometimes, it is required to test the app in multiple environments. If this is a requirement and you have to create one more virtual environment, then you can directly create it by right clicking on "Python Environments" and choose Add Virtual Environment as shown in the following image:
If you would like to install some other packages which are required for development, then just right click on any virtual environment and choose "Install Python Packages".
From the next screen, you can search your Python packages in the search box and click to Run command just below it for installing that Python package. For instance, search for "pypyodbc
" and click on "Run command: pip install pypyodbc" to install pypyodbc
.
Let's run the Django project and see the output. To run the project, you just need to press F5 from the Visual Studio and it will start compiling the project and here we go. Python interpreter has started to run the project.
That's the output as follows. Awesome, we are able to create our first Django Python project in Visual Studio and run it.
Conclusion
So, today we have seen how to create the first Django Python web application in Visual Studio.
I hope this post will help you. Please put your feedback using comment which helps me to improve myself for the next post. If you have any doubts, please post them in the comment section and if you like this post, please share it with your friends. Thank you.
You are here which means, you have read something useful. If you like this article, then please leave a comment below.
History
- 12th February, 2019: Initial version