How to Call a Function and Sending Parameter Value by clicking a Button in the Django Template File from a Django Application

Posted on

Introduction

Actually, this is a modification from the previous article of ‘How to Call a Function by clicking a Button in the Django Template File from a Django Application‘. After successfully calling a function by clicking a button exist in the Django template file, this article will show another further modification. Not only calling a function through the button click process but also sending a parameter value through the function. So that, the function can retrieve the parameter value and also process it further. Processing the parameter value in this context will be printing the parameter value.

How to Call a Function and Sending Parameter Value by clicking a Button in the Django Template File

So, this article will still use the example exist in ‘How to Call a Function by clicking a Button in the Django Template File from a Django Application‘. Actually, in order to achieve the purpose as exist in the title of this article, the steps is quite similar with the one exist in ‘How to Call a Function by clicking a Button in the Django Template File from a Django Application‘. Actually, it is useful as an additional reference. It will be easy to read it and then adopt it for later change or modification. In this case, the modification for sending a parameter value. Below are the steps in sequence :

  1. The first step is similar with the previous one. It starts by defining the method in ‘views.py’ file. Actually, the content will be slightly modified. The main purpose is to retrieve the parameter value. And also print that parameter value further. The aim is to prove that the parameter value passed is actually has the same one after it is being retrieved by printing it. So, below is the modification of the function ‘my_function()’ :

    def my_function(request, name):
        print("This is a click request from a Django template file with an argument : ", name)
        return render(request,"index.html")
    

    Rather than just a simple print process, it is also printing the parameter with the name ‘name’. Normally, if it is the original value from the parameter where it comes from the click button process, it will also print the same value in the function.

  2. The next file is also the similar one which is the ‘urls.py’ file. In this file, there is also a slight modification in order to fulfill the goal. It is the modification of the URL of ‘my_function’ so that it will retrieve an additional parameter as follows :

        path('my_function/<str:name>', views.my_function, name="my_function"),

    There is a slight modification in the above line. That slight modification is an additional ‘<str:name>’  in the end of the URL ‘my_function’ in order to define the part used for retrieving parameter value. Using the above line, below is the actual revision of the ‘urls.py’. It is actually to accommodate for accepting additional parameter value :

    from django.urls import path
    from apps import views
    urlpatterns = [
        path('', views.index, name="index"),
        path('my_function/<str:name>', views.my_function, name="my_function"),
    ]
  3. The last one, it is the Django template file. In this context, it is a file with the name of ‘index.html’. So, the following is the modification part :

    <a href="{% url 'my_function' 'My Parameter' %}"><button class="btn btn-primary" type="button">Call Function</button></a>Test

    In the above source code line, the modification is the additional part of ‘My Parameter’. In this example, that ‘My Parameter’ string acts as a parameter value.

  4. So, execute the Django application once more as follows :

    (env) C:\project\project>python manage.py runserver
    Watching for file changes with StatReloader
    Performing system checks...
    
    System check identified no issues (0 silenced).
    September 08, 2022 - 06:28:36
    Django version 4.1.1, using settings 'model.settings'
    Starting development server at http://127.0.0.1:8000/
    Quit the server with CTRL-BREAK.
    
  5. After the Django application is running, just click the button in the page. In this context, a page which is represented by the Django template file ‘index.html’. If all of the processes are running normally, there will be a print of a statement with the parameter value as follows :

    This is a click request from a Django template file with an argument : My Parameter
    [08/Sep/2022 13:08:32] "GET /my_function/My%20Parameter HTTP/1.1" 200 1485
    

Leave a Reply