添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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.

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.

per the include() example

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() 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.