Introduction
Let's start with the setup of the environment in the Django framework while installing it on Windows. The coding part is almost the same for any OS, either Linux, Windows, or MAC. Every process is formed in an image which will help us to move through the steps and create our first app. To create a web page or an app, we will choose Visual Studio 2010 as IDE.
First we will start the environment with Django.
As we have discussed in
my previous article, the following commands are used with this sequence.
python --version : Latest version
pip --version: Installer manager
django-admin --version:
mkvirtualenv base: When using this command we will create an environment as named "base".
Also we can set the virtual environment wrapper with the help of : pip install virtualenvwrapper-win
So, we can see now (base) is the environment where will execute our Django project files the the form of a Web application.
In case you didn't install Django then do it first. Then we will create a project folder.
Here, we have to work with latest version of Django. Use this command to check the version : django-admin --version 3.0.2
So, if you have another cmd open or apply the same command it will not work, because we had set Django for the environment, not for the operating system.
Now, we will move on and make a folder.
mkdir projects >> cd projects
Create a sub folder with name firstproject and check the directory files which have the Python framework and see if other settings files are already installed.
dir>> check the directory files.
runserver is the command which will introduce the Django on localhost: http://127.0.0.1:8000/
Now, the show begins. We want to change this page. It's our home page and want to write something here.
Here, we will go with Visual Studio IDE. Let's follow the below steps and see what happens. Let's create the app with Django:
Install the latest free community version of Visual Studio 2019.
After a few minutes, installation has completed. Now, choose the dark theme as you want to go with from the mentioned options, and follow the next steps as below.
We had already created a project. So, open a local folder and select project. You can see these files and folders in Solution Explorer.
We will check a few important files which which will help in us further applications and other development.
Let's check settings.py where we can find a secret key which is used for the deployment of the project while we are using the project on the server. Next is Debug=True, by default it's true. Debug contains a lot of information regarding projects and many data are stored inside. While you are deploying the project on the server make sure it's False, otherwise your informations can be opened and there are chances of hacking the server.
manage.py is a very crucial file where we will do some changes.
In this image, we can see that Amazon is a web application which has different modules which will make it as an actual application/project. Seller and buyer are main modules. It has another three modules Login, ChatBot and Login. These modules are called apps in Django.
Command Line with Visual Studio
Moving towards project coding. we will start with terminal or Command Line within the Visual Studio. Check the below image.
Apply the command python manage.py startapp calc . calc is the application name. Keep in mind you will get an error, the reason is we are using this with a different command line. So, that was in Windows Command Prompt and we will use here Visual Studio Command Line. Use a command workon base. This is our environment and now apply that command python manage.py startapp calc.
Check on the below image, and there an app/module calc is created with different files.
Django Mapping
Comapre all the files between the app and the parent folder. In calc, apps.py is also a very important file. We will see it soon.
In the first project folder you will find an urls.py, this is for the entire project.
Create a new file in calc app. Follow the below image.
Now we want to map our app with the project. We have to create a new file name as the same urls.py in parent folder.
Type the below code. Import path and views. urlpatterns are in list form, where path has the first argument as empty/home. Now we will navigate with a function home.
(After typing these codes you might get an error installing the extension, otherwise skip it. )
Let's move to another file to define the function home veiws.py in app calc.
By default render is imported on the page.
Now define the function request object. While we are using request it must be responsed.
So in the return line we will add HttpResponse and print the string "Hello World!"
We wil get an error, so import the HttpResponse which belongs to the module/package named http.
Now move to the firstproject >> urls.py
It the main url file you have to mentioned the mapping of the app calc.
Write in urlpatterns path(' ', include('calc.urls')),
After applying this it will throw an error, so import include with path .
Now referesh the localhost page of Django. If it will not work, then apply the command python manage.py runserver and try again .
It will be the home screen of the project.
This is not very attractive or big. But atvleast we have created a dynamic page with content. We will work on the fully functioning app very soon.
Summary
In this article, we learned about the Django environment setup, and we created a complete dynamic app/module from scratch. Also we discussed Django mapping and functions with the apps.vIntially, it's not much but it will provide us with the confidence to do a lot.