Introduction
When you say you want to use Django, it means you want to build a web application. There are two types of web application, we have static pages and then dynamic pages. Now, majority of the websites we visit nowadays are static pages meaning everyone will see the same thing. Dynamic pages are different for everyone. For example, if i go to Facebook, i will see my own feed and my friends feed which of course will be different from your feed. The same with our bank accounts, if i go to my bank account, i will see my bank balance, not your bank balance, so basically we have a dynamic content there. So when you say you want to build a website(static), you can use HTML, CSS and Javascript but to make it dynamic, you have to use Django. So lets build a simple web application. Lets do something where you can print “Hello world” on the page. Yes, we can do this with HTML but lets make it dynamic using Django.
To start, you have to use some sort of IDE, now basically, when you say IDE, you can also use notepad to type the code which is fine and you can also use sublime. Here, we are going to use Visual Studio Code. You can download Visual Studio Code by going to google, search for Visual Studio Code. Just go to the website and you can download the latest version. They are so many features so many developers use it. The community version is free while for the professional version, you have to pay for it but for now, we are happy to work with the free version, it works well, so kindly go ahead to download it and install on your system. The next step is to open it on your system and start working. In the previous article, we created a folder for our Django projects. We are going to open that project folder now in the Visual Studio Code. To do this, you have to go to the project folder using the file explorer, click on it and it shall be opened in Visual Studio Code. When you do this and click on the arrow near your project name, you should see the following:
pycache, init_py, settings.py, urls.py, wsgi.py, manage.py
Manage.py is useful to manage your project. If you want to do some settings in your project which is very important, you use Settings.py. If you click on it, you will see we have the SECRET_KEY which is used for deployment and we have DEBUG. DEBUG will give you a lot of information when you get stuck somewhere which is very important. When you are building a project and you want to know what’s happening behind the scene, just make “debug = True” and it will give you all the information but make sure before deploying on the server, you make it false, because someone could could hack your machine and they could see all the logs. We also have the INSTALLED APPS, that is, all the apps that are already installed on your project, we can also have our own apps. We also have the MIDDLEWARES which will help you for security purposes as well. We have the template, Django.template which will help you provide template stuff. We also have DATABASES which is required because if you want to build a web application, you also want to have certain databases behind the scene maybe MySQL, PostgreSQL or SQLite so by default Django gives you SQLite but you can also use PostgreSQL. We also have PASSWORDS which is used for validation. The settings.py file is usually not edited much so we wont do much there for now. We also have the urls.py file which is used for admin stuff.
Now, lets create our first app. Now, you may be wondering why you have to create a separate app when you are already in your project folder which is “practicals” which you already opened previously in Visual Studio Code. The thing we do in Django is we have a project right, let’s say you want to create a big web app where you have several features like admin stuff, product site, e-commerce website and so on meaning we have different sides to different apps and all the apps will be provided in one particular project which is your website in general. So all this apps will be one app. Login stuffs will be one app, home screen will be one app but coming together, they are all under one web application.
So let’s create a separate project here. The way you can do this is by going to your terminal in your VSC. In the terminal, we want to create an app, so we type “python manage.py startapp” then the name of the app at the back which is “python manage.py startapp calc”, then press “Enter”. This produces an error. The problem is now we are using a different environment, a different command prompt. Initially, we were using a windows command prompt but now, we are using a VSC command prompt. So to solve this issue, we use the former virtual environment we created in the command prompt which we named “test” in the VSC command prompt. In the VSC command prompt, you type “workon test”, then press “Enter” which will automatically solve the problem. Then you type “python manage.py startapp calc”, then “Enter”. This immediately creates a new app on the left hand side of the VSC app. You will clearly see that “calc” has been created.