# Understanding Django include()

**URL:** <https://forum.djangoproject.com/t/understanding-django-include/21788>\
**Category:** Getting Started\
**Created:** [June 22, 2023, 11:33pm UTC](https://forum.djangoproject.com/t/understanding-django-include/21788 "2023-06-22T23:33:20Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![strikeouts27](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/strikeouts27/32/13899_2.png) [@strikeouts27](https://forum.djangoproject.com/u/strikeouts27)\
**Post date:** [June 22, 2023, 11:33pm UTC](https://forum.djangoproject.com/t/understanding-django-include/21788/1 "2023-06-22T23:33:20Z")

</div>

I am having trouble understanding the include() function.

I am having trouble understanding the documentation for include(). I am currently working on the tutorial. Does include basically link the individual apps urls.py to the main urls.py of the project? Is that what it does? What does the documentation mean when it says includes chops off parts of the url and uses what is remaining and sends what remains to ??? where is it sending it to? Can I have an example?

What steps have I taken to solve the problem?  
I have tried asking Chat GPT.  
I have tried looking on Codecademy.  
I have tried looking at YouTube videos.  
I have tried asking the Django community on discord.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 23, 2023, 2:53am UTC](https://forum.djangoproject.com/t/understanding-django-include/21788/2 "2023-06-23T02:53:02Z")

</div>

> [@strikeouts27](#):
>
> Does include basically link the individual apps urls.py to the main urls.py of the project?

Quoting directly from the docs:

> At any point, your `urlpatterns` can “include” other URLconf modules.

So yes, it _can_ include an individual app’s urls.py file into your url structure, but it’s not _required_ that those included urls be from different apps. You _could_, if you so desired, create multiple URL conf modules in a single app.

> [@strikeouts27](#):
>
> includes chops off parts of the url and uses what is remaining and sends what remains to ??? where is it sending it to?

Again quoting directly from the docs:

> 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.**

The rest of the docs in that section are your example.

If you’re still wrestling with what the example is showing, please identify which part where you would like further clarification.

---

<div class="post-metadata">

**Author:** ![strikeouts27](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/strikeouts27/32/13899_2.png) [@strikeouts27](https://forum.djangoproject.com/u/strikeouts27)\
**Post date:** [June 27, 2023, 10:53pm UTC](https://forum.djangoproject.com/t/understanding-django-include/21788/3 "2023-06-27T22:53:54Z")

</div>

per the include() example

```auto
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path("polls/", include("polls.urls")),
    path("admin/", admin.site.urls),
]

```

under the path using the include statement it shows polls.urls.

according to the tutorial:  
The idea behind [`include()`](https://docs.djangoproject.com/en/4.2/ref/urls/#django.urls.include) is to make it easy to plug-and-play URLs. Since polls are in their own URLconf (`polls/urls.py`), they can be placed under “/polls/”, or under “/fun\_polls/”, or under “/content/polls/”, or any other path root, and the app will still work.

is that because the include function works similar to the name function and as long as polls.urls is somehow in their it will work? or does it work by seeing polls and ignores the urls.py portion? How does it know what to filter?

I was certain that was the name= feature.

> **[Django](https://docs.djangoproject.com/en/4.2/intro/tutorial01/)**
>
> The web framework for perfectionists with deadlines.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 27, 2023, 11:40pm UTC](https://forum.djangoproject.com/t/understanding-django-include/21788/4 "2023-06-27T23:40:50Z")

</div>

I’m sorry, I’m not following you at all here.

What “name” function are you talking about?  
If you’re talking about the `name` parameter to the `url` function, there’s no relationship between the two. They perform two completely separate and unrelated functions.

Can you elaborate on what you mean and are referring to by your other questions?

Also, have you read the linked docs to the `include` function?

---

<div class="post-metadata">

**Author:** ![strikeouts27](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/strikeouts27/32/13899_2.png) [@strikeouts27](https://forum.djangoproject.com/u/strikeouts27)\
**Post date:** [June 28, 2023, 1:00am UTC](https://forum.djangoproject.com/t/understanding-django-include/21788/5 "2023-06-28T01:00:25Z")

</div>

I was talking about the name parameter to the url function. Thank you for helping me undrstand that there is not a relation to the two. The name function I was taught is a reference point that django uses to identify which url path to use.

The instructions in the django tutorial mention that with include() I can name my url path whatever I want. “they can be placed under /polls/ fun\_polls etc…”

I am wondering why I can name my url pathway whatever I want using include.

Also there are linked docs to the include function? I do not see them in your response…

wait a minute I see a hyper link for include() in my response to you. ah… that is what you mean by the link. Yes I read it but I do not understand. Is this supposed to be a link between url config paths for a project?

Can someone explain the include function capabilites in simple terms?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 28, 2023, 1:25am UTC](https://forum.djangoproject.com/t/understanding-django-include/21788/6 "2023-06-28T01:25:59Z")

</div>

> [@strikeouts27](#):
>
> The name function I was taught is a reference point that django uses to identify which url path to use.

That’s not an accurate statement.

The name parameter (it’s not a function) is a name for a url, that’s all.

> [@strikeouts27](#):
>
> I am wondering why I can name my url pathway whatever I want using include.

That’s not what those docs are saying.

In the tutorial, you’re creating urls for the polls app. These urls are defined in step 3, in the section:

```auto
from django.urls import path
from . import views

urlpatterns = [
    # ex: /polls/
    path("", views.index, name="index"),
    # ex: /polls/5/
    path("<int:question_id>/", views.detail, name="detail"),
    # ex: /polls/5/results/
    path("<int:question_id>/results/", views.results, name="results"),
    # ex: /polls/5/vote/
    path("<int:question_id>/vote/", views.vote, name="vote"),
]

```

Notice that you’re only specifying the part of the url _after_ `polls` - because the name `polls` is defined in your root urls.py file like this:

```auto
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path("polls/", include("polls.urls")),
          ^^^^^
    path("admin/", admin.site.urls),
]

```

What the docs are saying is that the `polls` above doesn’t need to be `polls`. You can change that name to anything you want it to be (as long as it’s a valid url), and your application would still work.

In fact, you could have:

```auto
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path("polls/", include("polls.urls")),
    path("my-polls/", include("polls.urls")),
    path("admin/", admin.site.urls),
]

```

and you can then access `views.detail` for `question_id` 1 as _either_ `/polls/1/` _or_ `/my-polls/1/`.

---

<div class="post-metadata">

**Author:** ![strikeouts27](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/strikeouts27/32/13899_2.png) [@strikeouts27](https://forum.djangoproject.com/u/strikeouts27)\
**Post date:** [June 28, 2023, 2:57am UTC](https://forum.djangoproject.com/t/understanding-django-include/21788/7 "2023-06-28T02:57:47Z")

</div>

so in these lines of code we are defining the url after the polls/ pathway.

```auto
from django.urls import path
from . import views

urlpatterns = [
    # ex: /polls/
    path("", views.index, name="index"),
    # ex: /polls/5/
    path("<int:question_id>/", views.detail, name="detail"),
    # ex: /polls/5/results/
    path("<int:question_id>/results/", views.results, name="results"),
    # ex: /polls/5/vote/
    path("<int:question_id>/vote/", views.vote, name="vote"),
]

```

in these lines of code in my root urls.py file which I am led to believe is the projects urls.py file…  
you are saying I do not have to have it say polls/ it can be my-polls/ or andrew-polls/ or whatever so long as its a valid url. by that you must mean whatever comes after polls/ my-polls/ or andrew-polls/ whatever comes after that slash needs to also be the correct url.

````auto
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path("polls/", include("polls.urls")),
          ^^^^^
    path("admin/", admin.site.urls),
]```

I have two questions, 
I want to know is what is the function of include(), why do I need include(polls.urls) is that telling django to go to the polls.urls file and grab all of the pathways? I think that is what you are saying. 

Question two you mentioned that the url patterns that are NOT in the root directory but in the polls app urls... the ones listed below this line

````

from django.urls import path  
from . import views

urlpatterns = [  
# ex: /polls/  
path(“”, views.index, name=“index”),  
# ex: /polls/5/  
path(“int:question\_id/”, views.detail, name=“detail”),  
# ex: /polls/5/results/  
path(“int:question\_id/results/”, views.results, name=“results”),  
# ex: /polls/5/vote/  
path(“int:question\_id/vote/”, views.vote, name=“vote”),  
]

```auto
(continued) you said we were specifying what comes after the polls/. or my-polls/ or andrew-polls/

how exactly are we specifying what comes after polls/?
```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 28, 2023, 3:17am UTC](https://forum.djangoproject.com/t/understanding-django-include/21788/8 "2023-06-28T03:17:02Z")

</div>

> [@strikeouts27](#):
>
> whatever so long as its a valid url.

What I mean is that the characters being used in the url must be valid characters to be used in a url. (Must not be a space or any of a number of punctuation or special characters such as the question mark.)

Also to clarify, your “root urls” is the `urls.py` file defined in your `settings.py` file, regardless of the directory in which it resides. It identifies the first `urls.py` file to be processed by Django when it’s setting up the url structure.

---

<div class="post-metadata">

**Author:** ![strikeouts27](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/strikeouts27/32/13899_2.png) [@strikeouts27](https://forum.djangoproject.com/u/strikeouts27)\
**Post date:** [June 28, 2023, 4:54am UTC](https://forum.djangoproject.com/t/understanding-django-include/21788/9 "2023-06-28T04:54:43Z")

</div>

reading the documentation links again…

---

<div class="post-metadata">

**Author:** ![strikeouts27](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/strikeouts27/32/13899_2.png) [@strikeouts27](https://forum.djangoproject.com/u/strikeouts27)\
**Post date:** [June 28, 2023, 5:11am UTC](https://forum.djangoproject.com/t/understanding-django-include/21788/10 "2023-06-28T05:11:34Z")

</div>

In Django, `include()` is a function that allows you to insert the URLconf of another application into the current URLconf. This makes it easy to plug-and-play different Django applications. Suppose you have an application called `polls` with its own URL configurations. Rather than defining all the URL patterns in your main project’s `urls.py` , you can include the URLs from the `polls` app. Here’s how to do it:

```auto
from django.urls import include, path

urlpatterns = [
    path('polls/', include('polls.urls')),
    ...
]

```

In this example, any URLs starting with `polls/` will be forwarded to the `polls` application.

So basically include works like an import statement in python. It allows me to include all of the url paths of a urls.py file in another urls.py file in one line of code instead of importing everything. Is that correct?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 28, 2023, 12:05pm UTC](https://forum.djangoproject.com/t/understanding-django-include/21788/11 "2023-06-28T12:05:25Z")

</div>

Essentially correct, however it is not _limited_ to including a file by the name of `urls.py`.

Specifically, from the docs at [URL dispatcher | Django documentation | Django](https://docs.djangoproject.com/en/4.2/topics/http/urls/#including-other-urlconfs)

> At any point, your `urlpatterns` can “include” other URLconf modules.

and

> Another possibility is to include additional URL patterns by using a list of [`path()`](https://docs.djangoproject.com/en/4.2/ref/urls/#django.urls.path) instances.

See both examples in that section of the docs.

So while you might think of it as being similar to the `import` statement, the analogy only goes so far and you shouldn’t let your thinking be limited by it in that direction.

Finally:

> [@strikeouts27](#):
>
> In this example, any URLs starting with `polls/` will be forwarded to the `polls` application.

I would not describe this as being fully correct.

The URLs starting with `polls/` will be matched to the `urlpatterns` as defined in the `polls.urls` module. Whether or not the requests are actually handled by the `polls` app is an entirely different question and depends solely upon the contents of those urlpatterns.

---

<div class="post-metadata">

**Author:** ![strikeouts27](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/strikeouts27/32/13899_2.png) [@strikeouts27](https://forum.djangoproject.com/u/strikeouts27)\
**Post date:** [June 30, 2023, 6:57pm UTC](https://forum.djangoproject.com/t/understanding-django-include/21788/12 "2023-06-30T18:57:57Z")

</div>

I have also observed that when I am seeing a grayed out import statement in my import statement lines that one way to possibly fix it is to use the include() statement in my project urls.py.

path(‘url\_path\_name/’, include(‘appname.urls’)),

---

<div class="post-metadata">

**Author:** ![strikeouts27](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/strikeouts27/32/13899_2.png) [@strikeouts27](https://forum.djangoproject.com/u/strikeouts27)\
**Post date:** [July 6, 2023, 9:51pm UTC](https://forum.djangoproject.com/t/understanding-django-include/21788/13 "2023-07-06T21:51:55Z")

</div>

> [@KenWhitesell](#):
>
> URL dispatcher | Django documentation | Django

Per HydroMike in addition to linking url patterns we can also include files. Files can have an entire app in them with their own urls.py. Instead of typing out all of the url patterns in that respective file given to us include() will search that file for the urls.py and than “include” them into the search list for the projects urls.py file.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 6, 2023, 11:30pm UTC](https://forum.djangoproject.com/t/understanding-django-include/21788/14 "2023-07-06T23:30:11Z")

</div>

I don’t know who “HydroMike” is or what you’re referencing, but:

> [@strikeouts27](#):
>
> Instead of typing out all of the url patterns in that respective file given to us include() will search that file for the urls.py

It’s not clear what you’re trying to describe here or how this is different from the normal `include` statement.

---

<div class="post-metadata">

**Author:** ![strikeouts27](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/strikeouts27/32/13899_2.png) [@strikeouts27](https://forum.djangoproject.com/u/strikeouts27)\
**Post date:** [July 7, 2023, 5:20pm UTC](https://forum.djangoproject.com/t/understanding-django-include/21788/15 "2023-07-07T17:20:05Z")

</div>

I am trying to understand the include() app. I am leaving notes so that next time I have this issue I can follow my bookmarked questions and look them up.

Thank you for all of your help answering my questions so far Ken.

It is worth noting that in the path() method that when used with include will prepend the path name to the all the included urls.

like running a for each loop on each of the directorys that are passed in.

example: path(‘andrew/’, include(‘polls.urls’)

every path in polls.urls will be prepended with /andrew path.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 8, 2023, 12:21am UTC](https://forum.djangoproject.com/t/understanding-django-include/21788/16 "2023-07-08T00:21:33Z")

</div>

> [@strikeouts27](#):
>
> It is worth noting that in the path() method that when used with include will prepend the path name to the all the included urls.

You’re looking at this from the wrong direction. There’s no “prepending” being performed here.

Quoting directly from the docs at [URL dispatcher | Django documentation | Django](https://docs.djangoproject.com/en/4.2/topics/http/urls/#including-other-urlconfs:)

> Whenever Django encounters [`include()`](https://docs.djangoproject.com/en/4.2/ref/urls/#django.urls.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.

A request comes in from the browser. The browser may be submitting a request for a url like `/a/b/c/`.

If your path statement is `path('a/', include(b_app.urls))`, then Django is going to match the submitted `/a/b/c/` to this path. It’s then going to drop the `a/` from the request path and continue processing by searching the urlpatterns in `b_app.urls` with `b/c/`.

---

<div class="post-metadata">

**Author:** ![strikeouts27](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/strikeouts27/32/13899_2.png) [@strikeouts27](https://forum.djangoproject.com/u/strikeouts27)\
**Post date:** [July 8, 2023, 12:40am UTC](https://forum.djangoproject.com/t/understanding-django-include/21788/17 "2023-07-08T00:40:51Z")

</div>

okay so its coming in from the users http request into django and is getting filtered as it goes deeper into the website losing parts of the url when its already taken that pathway as it goes…

---

<div class="post-metadata">

**Author:** ![strikeouts27](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/strikeouts27/32/13899_2.png) [@strikeouts27](https://forum.djangoproject.com/u/strikeouts27)\
**Post date:** [July 13, 2023, 11:16pm UTC](https://forum.djangoproject.com/t/understanding-django-include/21788/19 "2023-07-13T23:16:34Z")

</div>

Note to self include does NOT link urls together.

when the request comes in from the browser it chops off the matching part and then tells django to look in the thing it redirects to.

---

<div class="post-metadata">

**Author:** ![strikeouts27](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/strikeouts27/32/13899_2.png) [@strikeouts27](https://forum.djangoproject.com/u/strikeouts27)\
**Post date:** [August 10, 2023, 5:59pm UTC](https://forum.djangoproject.com/t/understanding-django-include/21788/20 "2023-08-10T17:59:52Z")

</div>

would it be fair to say that include acts like a routing switch that chops off the parts of the path as it uses it?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 10, 2023, 6:09pm UTC](https://forum.djangoproject.com/t/understanding-django-include/21788/21 "2023-08-10T18:09:33Z")

</div>

Yea, I’d say that just about covers it.
