28
loading...
This website collects cookies to deliver better user experience
Views are the functions written in python as a logic control unit of the webserver
views.py
file inside of the application folder. The function name can be anything but should be a sensible name as far as its usability is concerned. Let's take a basic example of sending an HTTP response of 'Hello World'. from django.http import HttpResponse
def index(request):
return HttpResponse('Hello World')
urls.py
file in the application folder to create a map of the URL to be mapped with the view. After creating the file in the same app folder as the views.py
, import the function in the view into the file. from .views import index
from django.urls import path
urlpatterns = [
path('', index, name='index'),
]
urls.py
file of the project folder. from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('post.urls')),
]
path('', include('post.urls')),
and also import the include
function from django.urls
. This additional statement includes the urls or all the urlpatterns
in the post
app from the urls.py
file into the project's url-routes. 'home/'
, 'about/'
, 'posts/'
, etc. but since we are just understanding the basics, we'll keep it ''
i.e. the root URL. 'admin/'
which is a path to the admin section. We'll explore this path and the entire Admin Section in some other part of this series. http://127.0.0.1:8000
, you will see a simple HTTP message Hello World
. path( ' ', view, name )
^ ^ ^
| | |
| | url_name
| function_name
url_path
home/
, user/
, post/home/
, etc. and we can also have dynamic URLs like post/<pk:id>/
, user/<str:name>/
, etc. here the characters <pk:id>
and <str:name>
will be replaced by the actual id(integer/primary key) or the name(String) itself. View
as there are a lot of ways to create it, there are two types of views, how to use them for various use-cases that can be learned along the way because it is a topic where the crust of Django exists. def greet(request, name):
return HttpResponse('Welcome, ' + name)
name
and in response, it just says Welcome, name
where the name can be any string. Now after creating the view, we need to map the view to a URL pattern, We'll add a path for this greet function. path('greet/<str:name>/', greet, name='greet'),
<str:name>
is a variable or just a URL parameter to be passed to the view as the value of the variable name
. We have also given the URL map a name called greet, just for demonstration of its creation. from .views import index, greet
https://127.0.0.1:8000/greet/harry
, you should see a response Welcome, harry
as simple as that. <string:name>
which is like a URL parameter to the view. The path function automatically parses the name to the appropriate view and hence we call the greet function with the name variable from the URL. from random import randint
def dice(request):
number = randint(1,6)
return HttpResponse(f"Its {number}")
random.randint
function to generate the pseudo-random number between 1 and 6. We have used the f-string (f"{variable}"
)styled Response string as int is not compatible with the response concatenation. So this is the logic of our map, now we'll need to link it to a URL-path. path('throw/', dice, name='dice'),
from .views import dice
also add other views if present. Now if we go to the URL https://127.0.0.1:8000/throw/
, we shall see a random number in the response. This is how we used Python to make the logic of our view.