Introduction
This is a simple article describing about how to achieve the purpose exist as in the description of this article. In other words, this article will describe step by step to send input from an input text field to the function exist in Django application. Actually, there is an input from user where the user will insert it into a text field input. After clicking the button exist, it will send that input to the function in Django application. All of those components, starting from the text field input, the button for submitting the input exist in a form. Furthermore, the form will use a GET method for further processing. Basically, it use the setting exist in article ‘How to Change Default Page of a Django Application‘. It contains one application where it exist in the ‘apps’ folder. The following is the structure of the Django project :
C:\project>dir Volume in drive C is Windows-SSD Volume Serial Number is CA30-19A4 Directory of C:\project 09/06/2022 07:13 AM <DIR> . 09/06/2022 10:42 AM <DIR> .. 09/06/2022 08:19 PM <DIR> apps 08/24/2022 09:30 AM 159,744 db.sqlite3 08/20/2022 05:17 PM 683 manage.py 08/20/2022 05:18 PM <DIR> project 09/06/2022 07:14 AM 126 requirements.txt 3 File(s) 160,553 bytes 4 Dir(s) 65,556,508,672 bytes free C:\project>
Moreover, in the ‘project’ folder, just continue on by getting in to the ‘apps’ folder. The following is the content of the apps folder :
(env) C:\project\apps>dir Volume in drive C is Windows-SSD Volume Serial Number is CA30-19A4 Directory of C:\project\apps 09/06/2022 08:19 PM <DIR> . 09/06/2022 07:13 AM <DIR> .. 08/24/2022 10:02 AM 215 admin.py 08/24/2022 08:43 AM 146 apps.py 08/26/2022 02:19 PM <DIR> migrations 08/26/2022 02:05 PM 1,548 models.py 09/06/2022 07:01 PM <DIR> templates 08/24/2022 08:43 AM 63 tests.py 09/06/2022 08:03 AM 118 urls.py 09/08/2022 06:43 AM 871 views.py 08/24/2022 08:43 AM 0 __init__.py 09/08/2022 06:43 AM <DIR> __pycache__ 7 File(s) 2,961 bytes 6 Dir(s) 66,276,225,024 bytes free (env) C:\project\apps>
How to Send Input Text from Django Template File to Function exist in Django View using GET method
After getting a glimpse of the structure of the Django project and also the Django application exist. Below is the step for achieving the goal to be able to send input text from a Django template file. That input will be available as the Django application will pass it using GET method of a form to a function exist in the Django application. Below are the steps for achieving it :
-
First step, it is the step for designing the input for user to entry. In this context, it is available in the file with the name of ‘index.html’ which is available in the ‘templates’ folder of the ‘apps’ folder. Below is the source code of the input design using form, input text field and a button :
<form method="GET" action="{% url 'add_department' %}"> <div> Department Name : <input type="text" name="department_name" value=""></input> </div> <button class="btn btn-primary" type="submit">Add Department</button> </form>
-
Second step, it is to define the URL for processing the request to add the input from the user. In this context, input a Department’s name. Just open the ‘urls.py’ file of the Django application and add the URL as follows :
path('add_department/', views.add_department, name="add_department"),
So, including the above line definition as the URL for processing the request, it will be in the following format :
from django.urls import path from apps import views urlpatterns = [ path('', views.index, name="index"), path('add_department/', views.add_department, name="add_department"), ]
-
Third step, it is to define the function where it exist as in the URL definition above. As in the URL definition above for URL with the name of ‘add_department’, the process will go further to a method with the name of ‘add_department’ as part of the ‘views’. It exist in the ‘views.py’ file located inside the ‘apps’ folder which represent the Django application folder. Below is the actual content for defining the ‘add_department’ function for processing the request :
def add_department(request): if request.GET: name = request.GET.get('department_name') print("Department Name : ",name) else: print("Not a GET request") return render(request,"index.html")
-
After that, just execute the Django application so that it will display the following output :
C:\project\apps>python manage.py runserver Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). September 09, 2022 - 13:45:39 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.
-
Access the page with the URL of ‘/add_department’ in order to demonstrate for sending a GET request. Below the page will appear :
-
Next, just continue on by inserting a sample of text which is ‘My Department’ in this case. Below is the appearance of the display of the page along with the URL of the page after the entry process :
Furthermore, there is an output exist which is proving that the parameter with the value of ‘My Department’ is succeeded on being passed to the function exist in Django application. As a prove, the following is the continuation of the above output showing the value of the parameter value which is ‘My Department’ :
Department Name : My Department [09/Sep/2022 13:45:48] "GET /add_department/?department_name=My+Department HTTP/1.1" 200 507
One thought on “How to Send Input Text from Django Template File to Function exist in Django View using GET method in Django Application”