A view function or view in short is like a Python function that a Web request and returns Web response.This response can be the HTML contents of a Web page, or a redirect, or a 404 error, or an XML document, or an image . . . or anything, really.
When we created our polls app a views.py file was created in polls directory, we gonna write all our views here.
Open the file polls/views.py and put the following Python code in it:
from django.http import HttpResponsedef index(request):return HttpResponse("You are at the polls index")
I am assuming you know basics of Python.
This is the simplest view possible in Django. Here we defined a view function index (you can name it anything), we call it a view function because, it takes a request(which is a compulsory argument for view functions) and returns a response which is a string here.
But we need to map our view to a URL (which will be requested) so that it can response accordingly.
To create a URLconf in the polls directory, create a file called urls.py
Now Go to polls/urls.py and clear it's code and put this one:
from django.urls import pathfrom . import viewsurlpatterns = [path('', views.index),]
- We use . to import files in same directory, and here we used it to import views. Also urlpatterns is a list which will contain all the urls related to our polls app.
- The path() function is passed four arguments, two required:routeandview, and two optional:kwargs, andname.
- routeis a string that contains a URL pattern. When processing a request, Django starts at the first pattern in urlpatterns and makes its way down the list, comparing the requested URL against each pattern until it finds one that matches.
- When Django finds a matching pattern, it calls the specified view function with an HttpRequest. Like in our case it calls index view function.We will talk about other two arguments later.
The next step is to point the root URLconf i.e. the main path or url.
In mysite/urls.py, add an import for django.urls.include and insert an include() in the urlpatterns list, so you have:
from django.contrib import adminfrom django.urls import include, pathurlpatterns = [path('polls/', include('polls.urls')),path('admin/', admin.site.urls),]
The include() function allows referencing other URLconfs. This will get more clear in a while.
You have now wired an index view into the URLconf. Lets verify it’s working, run the following command:
...\> py manage.py runserver
Go to http://127.0.0.1:8000/polls in your browser, and you should see the text, which you defined in the index view.
OK let see how all this works to understand better.
When a user goes to http://127.0.0.1:8000/polls Django looks into urlpatterns in root urls.py file (mysite/urls.py) . When Django reach to polls path, include() function sends it to polls/urls.py file, there it matches with our first path, then Django get directed to index view in views.py file, where index return a string as HttpResponse which you see on your screen at http://127.0.0.1:8000/polls
When to use include() : You should always use include() when you include other URL patterns. admin.site.urls is the only exception to this.
So we just learned basics of how views and urls works in Django. You are doing great keep it up!