Django day 4
What are Views and URLs in Django?
Welcome to day four of our Django tutorial series! If you missed the previous tutorials, you can find the link to day three’s tutorial here.
Today, we will be learning about What are Views and URLs in Django
Views
If you’re, familiar with html documents, Django Views are python functions that takes up http requests and returns https response, just like HTML documents.
Django views can be used to handle a wide range of requests, including rendering HTML pages, serving JSON or XML data, processing form submissions, and handling AJAX requests. Views are a key component of the Model-View-Controller (MVC) architecture used by Django and many other web frameworks.
URLS
In Django, URLs (Uniform Resource Locators) are used to map HTTP requests to specific view functions or classes that can handle them. URLs define the structure of a web application’s URLs and provide a way to navigate between different pages or views.
Django’s URL routing system provides a powerful and flexible way to organize a web application’s URLs and handle incoming requests. By defining URL patterns and associating them with view functions or classes, developers can create a well-organized and maintainable web application that is easy to navigate and use.
for Example,
first we need to create views.py file.
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello, World!, Its me SID")
the above function hello, takes request as an input object and returns an ‘HttpsResponse’ object that contains the string.
Now lets define urls.py
from django.urls import path
from . import views
urlpatterns = [
path('hello/', views.hello, name='hello'),
]
Output looks like this:
Thank you for joining today’s tutorial. While this example was basic, we will delve into more complex structures in upcoming tutorials.
I hope you found the content helpful, and please don’t hesitate to reach out to me if you have any questions or concerns.
Additionally, if you enjoyed this tutorial, consider following me for more valuable content. Thank you!