Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Django, how can I understand include() function?

Tags:

python

django

I have started learning Django, I'm not sure what the include() function means.

Here is mysite/urls.py. - project

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^polls/', include('polls.urls')),
    url(r'^admin/', admin.site.urls),
]

Here is polls/urls.py. - app in project

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
]

From django document, include() function was described as follows.

Whenever Django encounters include(), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.

I'm not sure what is that point, what is remaining string.

In case of the example above, what is remining string, what is url strings which was chopped off?

like image 622
Yuiry Kozlenko Avatar asked Mar 08 '26 20:03

Yuiry Kozlenko


1 Answers

For example, from this URL:

polls/5/results

the URL rule:

url(r'^polls/', include('polls.urls')),

chops off the polls/ part of URL and sends the remaining string after polls/, whatever it might be, for example (see here more):

5/results/

to urls from the poll app's urls.py, where it will then be mapped to a view based on the URL rules defined in this file

like image 87
doru Avatar answered Mar 11 '26 10:03

doru