Read, study and learn about everything important in your life
Everything is important, learning everywhere is block
There is a urls.py file in Django, which is used to manage django urls and routes. We can create or modify routes in the urls.py file to achieve different view functions for different urls.
First look at the urls.py long sample?
By default, django has only one route, admin. When we open manage.py, we can only access the 127.0.0.1:8000/admin/ directory, and the rest cannot be accessed.
As you can see, the creation of a route conforms to the rules of a regular expression, ^ means start, and $ means end;
In addition, the matching of the routes is from top to bottom, that is, the matching is started from the first route, and if it is satisfied, it does not match, if it does not match, it continues to go down. Therefore, in order to avoid accessing the url that does not exist, resulting in an error, you can add a route matching any url, you can jump to the 404 page (defined by yourself), or you can jump to the home page.
Generally when we want to pass the argument, the url is similar: 127.0.0.1/app/?a=1&b=2
At this point the configuration of urls.py is like this:
At this point the view.py code looks like this:
However, if we want to change the URL to: 127.0.0.1/app/1/2/?
We see that there is a name field in the routing configuration in urls.py, but it is optional, but it is recommended to write it.
Explanation: name is equivalent to giving this route a name. The advantage is that the regularity of the route may change frequently. The url in the html also needs to change, because it needs to correspond to the route in urls. But if you give the route a name, you can use the name of the route in the html, so that when the regularity of the route changes, but as long as the name does not change, html does not need to be changed.
This can be used in an HTML page: