Home » Cloud Technologies » Writing first Django Application

Writing first Django Application

This post is in continuation with our previous post “Starting webserver using your first simple Django project” , where we showed how we can start the webserver. Now, as we have the webserver running, lets see how we can start an application which will serve the custom built usecase over this webserver.

$ cd django_container

Your apps can live anywhere on your Python path. In this post, we’ll create our helloworld app in the same directory as your manage.py file so that it can be imported as its own top-level module, rather than a submodule of project.

$ python manage.py startapp my_helloworld_django_app

As we created the app, we can see it got created beside our project “my_django_project” in same directory “django_container”

$ ls
db.sqlite3  env  manage.py  my_django_project  my_helloworld_django_app
$ tree my_helloworld_django_app/
my_helloworld_django_app/
├── admin.py
├── apps.py
├── __init__.py
├── migrations
│ └── __init__.py
├── models.py
├── tests.py
└── views.py

As we start writing our first application, the first thing we need to do is to design how we want to show it to the end user, this design needs to be done in our applications views. ( i.e. my_helloworld_django_app/views.py in this example )

Write the app views

$ vim my_helloworld_django_app/views.py
from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world from Django First App!")

here, in our first django app, we intend to just show some string “Hello, world from Django First App” as http response when someone visits url in the server. i.e. it will be a http request.

Link the app views to app urls

Now, we need to link the views we created for the app to the app’s urls, so that the urls gets redirected to appropriate views. In our example, app’s url file is “my_helloworld_django_app/urls.py” . Since this file was not created by default for app, we need to create it,

$ vim my_helloworld_django_app/urls.py
from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

As we can see above “from . import views” using this line, we imported the views i.e. view.py from the current directory i.e. app directory. and using the line “path(”, views.index, name=’index’)” we mapped this url to views index function.

The path() function is passed four arguments, two required: route and view, and two optional: kwargs, and name. At this point, it’s worth reviewing what these arguments are for.

path() argument: route

route is a string that contains a URL pattern. When processing a request, Django starts at the first pattern in urlpatterns and makes its way down the list, comparing the requested URL against each pattern until it finds one that matches.

Patterns don’t search GET and POST parameters, or the domain name. For example, in a request to https://www.example.com/myapp/, the URLconf will look for myapp/. In a request to https://www.example.com/myapp/?page=3, the URLconf will also look for myapp/.

path() argument: view

When Django finds a matching pattern, it calls the specified view function with an HttpRequest object as the first argument and any “captured” values from the route as keyword arguments. We’ll give an example of this in a bit.

Mapping project urls to App urls

Now, we are ready to map the projects urls to the app’s url. So that when ever the user visits the url, which is directly linked to project’s url, internally its goes on to refer app’s urls and then present the views mapped to app’s url.

$ vim my_django_project/urls.py
from django.urls import include, path
urlpatterns = [
    path('helloworld/', include('my_helloworld_django_app.urls')),
]

Here, we mapped, the “helloworld” url i.e. http://127.0.0.1:8000/helloworld to “my_helloworld_django_app” app’s “urls.py” using below line,

path('helloworld/', include('my_helloworld_django_app.urls')) 
$ python manage.py runserver

Now, if you visit the url http://127.0.0.1:8000/helloworld , you should see the web page with message as “Hello, world from Django First App!”


Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

Leave a Comment