Django

Create Hello World View and Map it to a URL

πŸ“Œ Introduction

In Django, a view is a function that handles user requests and returns a response.
In this program, we will create a simple Hello World application and connect it with a URL.


🎯 Program Statement

πŸ‘‰ Create a simple Hello World view and map it to a URL.


βš™οΈ 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 (Add this line):

INSTALLED_APPS = [
...
'myapp',
]

βš™οΈ Step 3: Create Hello World View


πŸ“ File: views.py

πŸ”Ή Path:

myproject/myapp/views.py

πŸ”Ή Code:

from django.http import HttpResponse

def hello(request):
return HttpResponse("Hello World from Django!")

βš™οΈ Step 4: Configure 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('hello/', views.hello),
]

βš™οΈ Step 5: Run Server

python manage.py runserver

🌐 Step 6: Check Output

Open browser:

πŸ‘‰ http://127.0.0.1:8000/hello/

βœ… Output:

Hello World from Django!

🧠 Explanation

  • View β†’ Function that returns response
  • HttpResponse β†’ Sends data to browser
  • URL Mapping β†’ Connects URL to view
  • path(‘hello/’, views.hello) β†’ Calls hello() when URL is accessed

πŸ” Alternative (Better Practice)

You can also make homepage:

path('', views.hello)

πŸ‘‰ Then open:

http://127.0.0.1:8000/

⚠️ Common Errors


❌ Error: view not found

πŸ‘‰ Ensure:

from myapp import views

❌ Error: Page not found (404)

πŸ‘‰ Check URL:

/hello/

(not /hello)


❌ Forgot to register app

πŸ‘‰ Add 'myapp' in INSTALLED_APPS


πŸ§ͺ Practice Questions

  1. Create a view that displays β€œWelcome MCA Students”
  2. Map it to /welcome/
  3. Make it homepage
  4. Create two views and map different URLs

🎀 Viva Questions & Answers


1. What is a view in Django?

A view is a Python function that takes a request and returns a response. It contains the logic of the application.


2. What is HttpResponse?

HttpResponse is used to send data back to the browser. It can return text, HTML, or JSON.


3. What is URL mapping?

URL mapping connects URLs to view functions. It determines which function runs for a given URL.


4. What is the role of path()?

The path() function defines URL patterns. It maps a URL route to a specific view.


5. What is request object?

The request object contains all information about the user request. It includes data like method, headers, and parameters.


6. What happens when a user enters a URL?

Django matches the URL with urls.py, calls the corresponding view, and returns a response to the browser.


7. Difference between function-based view and class-based view?

Function-based views use simple Python functions, while class-based views use classes and provide more reusable and structured code.


8. What is the default URL file in Django?

urls.py is the default file used for URL routing. It defines all URL patterns of the project.


9. Why do we import views in urls.py?

To connect URLs with the corresponding view functions. Without importing, mapping is not possible.


10. Can we map multiple URLs to same view?

Yes, multiple URL patterns can point to the same view. This allows flexibility in routing.


πŸ”— Navigation

πŸ‘‰ Next Post: Create Multiple Pages (Home, About, Contact)
πŸ‘‰ 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 *