Django URL MappingWell, till here, we have learned to create a model, view, and template. Now, we will learn about the routing of application. Since Django is a web application framework, it gets user requests by URL locater and responds back. To handle URL, django.urls module is used by the framework. Let's open the file urls.py of the project and see the what it looks like: // urls.py See, Django already has mentioned a URL here for the admin. The path function takes the first argument as a route of string or regex type. The view argument is a view function which is used to return a response (template) to the user. The django.urls module contains various functions, path(route,view,kwargs,name) is one of those which is used to map the URL and call the specified view. Django URL FunctionsHere, we are giving some commonly used functions for URL handling and mapping.
Let's see an example that gets user request and map that route to call specified view function. Have a look at the steps. 1. Create a function hello in the views.py file. This function will be mapped from the url.py file. // views.py // urls.py Now, start the server and enter localhost:8000/hello to the browser. This URL will be mapped into the list of URLs and then call the corresponding function from the views file. In this example, hello will be mapped and call hello function from the views file. It is called URL mapping. Next TopicStatic Files Handling |