S
S
SergiusGei2018-06-17 10:50:49
Django
SergiusGei, 2018-06-17 10:50:49

How is the django1.10 url built correctly?

urlpatterns is a list from url(), so it's built like this:


urlpatterns = [
url(...),
url(...),
]
. question 1. How is
url() built ? -9]{4})/([0-9]{2})/$', 'month_archive'), url(r'^articles/([0-9]{4})/([0-9 ]{2})/([0-9]+)/$', 'article_detail'), ] In this example, articles is the name of a directory or file? can a directory or document name be a module? I don't understand at all

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Tim, 2018-06-18
@SergiusGei

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 question

Ask a Question

731 491 924 answers to any question