“Hello World” Using Django (First App)

Ashish Sharma
4 min readMar 27, 2018

Ya I know the blogs are coming a little late but that is due to my college work. Also because of excess content sharing (according to Facebook), I was restricted from sharing my blog/video in any group until today morning. So as soon as the regulation was over, I shared my video regarding CSS into different groups and got back to Django. We created our first project in Django and ran our server in the last blog. Today, we will learn how to write the most famous two words in the history of programming- “Hello World”. So let’s jump into our first application in Django!

As I explained in the previous blog, Django application is a part of a Django Project. There would be various applications which form together a project. For example, one application could just get the data while other could forward it to the receiver. So, each application has its own usage. To create an application we have to use the manage.py available in our project. So we can change the directories and use the manage file as shown below:

FirstApp is the name of our application.

We will have a migrations folder along with following files as we had earlier during the creation of our project:

Following is the description of the files shown above:

__init__.py will be a blank script similar to our project file.

We can register our models in the admin.py for interfacing.

Application specific configurations are placed in apps.py.

The data models for applications are stored in models.py.

Test cases for our application can be made available in tests.py.

Views.py consists of functions which handle requests and return responses.

Migrations contain database-model specific information.

Now we have created our application but our project has no idea about it. So we need to tell our project that we have an app named “FirstApp” which we have just created. We can tell our project this by adding the name of our app in the settings.py file of our “FirstRun” as shown:

Now if we try to run our server and it runs without any error, it means that our application has been added successfully. For now, our application is doing nothing. In order to make our application work, we have to create a view. A view is a function which takes a web request and returns a response. The response is returned with the help of HttpResponse which we need to import, so we can display our “Hello World” by adding the following in views.py in FirstApp as shown:

So our app is ready, our content is ready and now we just need a URL for our application. For this, we will be using urls.py under the FirstRun. Since our views are under the FirstApp, we have to first import them. After imporing, we have to add our URL in urlpatterns[] (path for admin would be already available there). Previously, the syntax was very complex but now it has been made simpler as shown:

url(r’^$’,views.function_name,name= ‘abc’) //earlier

path(‘pathname/’,views.function_name,name= ‘abc’) //now

The URL would work even without the name, it is optional. So, after adding everything our urls.py would look like:

So now if we visit the specified link with ‘hello’, we can have our display as shown:

It is zoomed to 400%, hence it is appearing so big!

So, we will learn more about the urls in the coming blogs. I know, I am taking a lot of time in learning these, but I am keeping it slow and steady. Be happy, stay healthy and I will see you in the next blog, till then keep coding…

Thank you for reading, if you liked the article please click on the clap button and show your support. You can clap more than once, just keep pressing the icon!!!

--

--