π Introduction
Forms are used to take input from users.
In Django, the GET method sends data through the URL.
In this program, we will:
- Create a form
- Accept user name
- Display greeting message
π― Program Statement
π Create a GET form that accepts a user name and displays a greeting message.
βοΈ Step 1: Create Project and App
django-admin startproject myproject
cd myproject
python manage.py startapp myapp
βοΈ Step 2: Register App
π File: settings.py
πΉ Path:
myproject/myproject/settings.py
πΉ Code:
INSTALLED_APPS = [
...
'myapp',
]
βοΈ Step 3: Configure Templates
π File: settings.py
πΉ Path:
myproject/myproject/settings.py
πΉ Code:
import os
TEMPLATES = [
{
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
},
]
βοΈ Step 4: Create View
π File: views.py
πΉ Path:
myproject/myapp/views.py
πΉ Code:
from django.shortcuts import render
def greet(request):
name = request.GET.get('name')
return render(request, 'greet.html', {'name': name})
π§ Explanation
request.GET.get('name')β gets value from form- If user enters name β it will be displayed
- Data is passed to template
βοΈ Step 5: URL Mapping
π File: urls.py
πΉ Path:
myproject/myproject/urls.py
πΉ Code:
from django.contrib import admin
from django.urls import path
from myapp import views
urlpatterns = [
path('admin/', admin.site.urls),
path('greet/', views.greet),
]
βοΈ Step 6: Create Template
π File: greet.html
πΉ Path:
myproject/templates/greet.html
πΉ Code:
<!DOCTYPE html>
<html>
<head>
<title>Greeting Form</title>
</head>
<body>
<h1>Enter Your Name</h1>
<form method="get">
<input type="text" name="name" placeholder="Enter your name">
<button type="submit">Submit</button>
</form>
<hr>
{% if name %}
<h2>Hello, {{ name }}!</h2>
{% endif %}
</body>
</html>
π§ Explanation
<form method="get">β sends data via URLname="name"β key used inrequest.GET{% if name %}β checks if user entered value
βοΈ Step 7: Run Server
python manage.py runserver
π Step 8: Output
Open:
π http://127.0.0.1:8000/greet/
β Step 1:
User enters: Kavita
β URL becomes:
http://127.0.0.1:8000/greet/?name=Cherry
β Output:
Hello, Cherry!
π§ How It Works
- Form sends data using GET
- Data appears in URL
- Django reads using
request.GET - Value passed to template
- Template displays greeting
β οΈ Common Errors
β Name not showing
π Check:
request.GET.get('name')
β Form not submitting
π Ensure:
<form method="get">
β Wrong variable name
π Must match:
name="name"
π§ͺ Practice Questions
- Display βWelcome, <name>β instead of Hello
- Add default message if no name entered
- Take two inputs (name + city)
- Display greeting with both values
π€ Viva Questions & Answers
1. What is GET method?
GET method sends data through the URL. It is mainly used to retrieve data from the server.
2. What is request.GET?
request.GET is a dictionary that contains data sent using GET method. It allows access to user input.
3. Difference between GET and POST?
GET sends data in URL and is less secure, while POST sends data in request body and is more secure.
4. What is form in Django?
A form is used to collect user input. It can send data to the server using GET or POST method.
5. Why do we use name attribute in input?
The name attribute acts as a key. Django uses it to retrieve the value using request.GET.
6. What happens if user does not enter input?
The value becomes None. We handle it using {% if %} condition.
7. What is {% if %} in template?
It is used to apply conditions in templates. It controls when content should be displayed.
8. Can we send multiple values using GET?
Yes, multiple inputs can be sent using GET method. Each input has a different name.
9. What is query string?
The part after ? in URL is called query string. Example: ?name=Kavita.
10. Is GET method secure?
No, GET is not secure because data is visible in the URL. It should not be used for sensitive data.
π Navigation
π Next Post: Marks Analysis (Total, Average, Highest, Lowest)
π Back to List: Django Programs (60 Questions with Solutions)
Further Reading
Introduction to Django Framework and its Features
Examples of Array Functions in PHP
Registration Form Using PDO in PHP
Inserting Information from Multiple CheckBox Selection in a Database Table in PHP
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
