Django

Create a GET Form to Accept User Name and Display Greeting

πŸ“Œ 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 URL
  • name="name" β†’ key used in request.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

  1. Form sends data using GET
  2. Data appears in URL
  3. Django reads using request.GET
  4. Value passed to template
  5. 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

  1. Display β€œWelcome, <name>” instead of Hello
  2. Add default message if no name entered
  3. Take two inputs (name + city)
  4. 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

Django Practice Exercise

Examples of Array Functions in PHP

Basic Programs in PHP

Registration Form Using PDO in PHP

Inserting Information from Multiple CheckBox Selection in a Database Table in PHP

programmingempire

princites.com

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *