Answer the question
In order to leave comments, you need to log in
How is the django1.10 url built correctly?
urlpatterns is a list from url(), so it's built like this:
. question 1. How is
urlpatterns = [
url(...),
url(...),
]
Answer the question
In order to leave comments, you need to log in
Of course, it's better to switch to 2.0, there you no longer need to use regular expressions to filter values in the url. Look sexy:
In principle, in 1.10 the same is just what is cut out using regular expressions.
The logic of the dispatcher is approximately the following:
1. The web server receives the url to which the user has been redirected (eg 127.0.0.1:800/user/4/albums/12);
2. The web server calls the url_dispatcher function and passes the path without the IP address as an argument;
3. The dispatcher starts looping through your url_patterns dictionary from top to bottom, and applies the filter until it's True. The filter is what comes first, in your case in 1.10 it's a regular expression. And when it is True, then the view.NAME function is called and the parameters from the regular expression are passed into it in the form of arguments.
4. View receives arguments and the request object and, logically, collects the response, takes out the template and puts the context there in the form of a dictionary
5. You can cycle through the dictionary already in the template or by directly mentioning it via {{ name }}
PS what is specified in the url as name="list_albums' is needed to generate a link to the template using the {% url 'list_albums' %} function. You can insert a link on the template by calling it by the name that you entered in name.They recommend using this in order not to form the link by hand, since in case of changing the url you need to go through all the templates and change everything everywhere.
And I want to add that in Django I myself am a noob, and until I started reading the documentation, I, like you, googled and looked for answers. This is very inefficient for development. It is better to follow the guide and understand why it works the way it does. For example, I liked the guide on djangogirls, but now with the release of 2.0 it is outdated and the documentation for 2.0 has a good Tutorial, although not complete. It's unclear what to do with authorization.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question