How to Setup and Work with Python for Web Development with Django Framework ?
What is Python?
Python is a popular and versatile programming language that can be used for web development, data analysis, machine learning, and more.
Django is a high-level web framework that follows the model-view-template (MVT) pattern and allows you to create dynamic and scalable web applications with Python.
Setting Up the Environment for Python and Django
You will need to install the following tools: – Python: You can either
- Either Download the latest version of Python from https://www.python.org/downloads/ and follow the installation instructions for your operating system. Or,
- Open Microsoft Store Application and type Python in the search bar when you’ll get all the results for the Python and currently Python 3.11 is the latest that you can download and install on your machine following the instructions. (Note: Personally, I found getting the software from Microsoft Store is the better option as it takes care of all the configuration for you)
- Pip: Pip is a package manager that lets you install and manage Python libraries and modules. Pip is usually included with Python, but you can also check if it is installed by running `pip –version` in your terminal or command prompt.
- Virtualenv: Virtualenv is a tool that lets you create isolated Python environments for different projects. This way, you can avoid conflicts between different versions and dependencies of Python packages. You can install virtualenv by running `pip install virtualenv` in your terminal or command prompt
- Django: Django is a web framework that you can install using pip. To install the latest version of Django, run `pip install django` in your terminal or command prompt.
Once you have installed these tools, you are ready to create your first Django project. Here are the steps to follow:
- Create a new directory for your project and navigate to it in your terminal or command prompt.
- Create a new virtual environment for your project by running virtualenv env, where env is the name of your environment. This will create a new folder called env inside your project directory that contains the Python interpreter and the packages for your project.
- Activate your virtual environment by running env\Scripts\activate on Windows or source and env/bin/activate on Linux or Mac OS. You should see (env) at the beginning of your terminal or command prompt line, indicating that you are using the virtual environment.
- Create a new Django project by running django-admin startproject projectname, where projectname is the name of your project. This will create a new folder called projectname inside your project directory that contains the files and folders for your Django project.
- Navigate to your project folder by running cd projectname in your terminal or command prompt.
- Create a new Django app by running python manage.py startapp appname, where appname is the name of your app.
- Run python manage.py migrate in your terminal or command prompt to apply the initial database migrations for your project.
Q) I wish to use Visual Studio Code for this Python and Django web applicationn! Does it change anything above ?
No, it does not change anything above. Visual Studio Code is a code editor that you can use to write and edit your Python and Django code. You can download and install Visual Studio Code from
https://code.visualstudio.com/ and follow the installation instructions for your operating system.
To use Visual Studio Code for your Django project, you will need to install some extensions that will enhance your coding experience. Here are some recommended extensions:
Python: This extension provides syntax highlighting, code completion, debugging, testing, and other
features for Python development. You can install it by clicking on the Extensions icon on the left sidebar
of Visual Studio Code and searching for Python. Then click on the Install button next to the extension
name.
Django: This extension provides syntax highlighting, code snippets, and other features for Django
development. You can install it by clicking on the Extensions icon on the left sidebar of Visual Studio
Code and searching for Django. Then click on the Install button next to the extension name.
Pylint: This extension provides code analysis, error checking, and code quality improvement for Python
development. You can install it by clicking on the Extensions icon on the left sidebar of Visual Studio
Code and searching for Pylint. Then click on the Install button next to the extension name.
After installing these extensions, you can open your project folder in Visual Studio Code by clicking on the
File menu and selecting Open Folder. Then browse to your project directory and click on Select Folder. You should see your project files and folders in the Explorer panel on the left sidebar of Visual Studio Code. To run your Django project in Visual Studio Code, you will need to configure a launch configuration file that tells Visual Studio Code how to run your project. Here are the steps to follow:
Click on the Run and Debug icon on the left sidebar of Visual Studio Code and then click on Create a
launch.json file.
Select Python from the list of environments and then select Django from the list of configurations.
A new file called launch.json will be created in a folder called .vscode inside your project folder. This
file contains the settings for running and debugging your Django project in Visual Studio Code.
Save and close the file.
To run your project, click on the green Run button at the top of Visual Studio Code or press F5 on your
keyboard. This will start a local web server that hosts your Django project at http://localhost:8000/.
To stop your project, click on the red Stop button at the top of Visual Studio Code or press Shift+F5 on
your keyboard.
That’s it! You have successfully set up your environment for Python and Django development using Visual
Studio Code. Happy coding! 😊