简体   繁体   中英

What's the meaning of order in a django request processing?

OVERVIEW

Consider the below urls.py:

from django.contrib import admin
from django.urls import re_path, include
from django.conf.urls.static import static
from django.conf import settings

from django.contrib import admin
from django.urls import path

a)
urlpatterns = [
    path('admin/', admin.site.urls),
]

# b)
# urlpatterns = [
#     path('admin/', admin.site.urls),
# ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

print(urlpatterns)

As you can see we got 2 url lists, whose __str__ representation is:

a) [<URLResolver <URLPattern list> (admin:admin) 'admin/'>]
b) [<URLResolver <URLPattern list> (admin:admin) 'admin/'>, <URLPattern '^media\/(?P<path>.*)$'>] 

PROBLEM

If I make the same request to localhost:8000 it's producing 2 different outcomes for the abovementioned url lists:

  • Using a) , I'll get a 200 response:

在此处输入图片说明

  • Using b) , I'll get a 404 response:

在此处输入图片说明

QUESTION

After reading the section "How django processes a request" I've observed it says in point 3):

Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL.

What does the concept of "order" means in this context?

For instance, let me put you an analogy of what I understand of "order processing", if i declare a couple of lists (b is extended from a), a=[0,1,2,3]; b=a+[4,5,6] a=[0,1,2,3]; b=a+[4,5,6] and then i do a.index(2)=2, b.index(2)=2 , that type of order processing (linear search) is what i'd also expect in django.

So I'm failing miserably to understand how the request is processed in the exposed case. For instance, I'd expect the same match in b) than in a) (ie: same response 200 in b) than in a) )

So, could anyone please explain me how this particular request is processed in both cases and why in a) is matching while in b) is not? Also, it'd be helpful to know how to print somehow the order of url attempts even if the response is 200 so one could learn much better about this important topic.

Btw, reading https://docs.djangoproject.com/en/2.0/ref/urls/#static didn't clarify too much about this doubt neither.

You are trying to access http://localhost:8000/ , but at the moment there aren't any matching URL patterns for that. You one URL pattern is for the Django admin, which you can access at http://localhost:8000/admin/ .

When Django doesn't find a matching URL pattern and you have DEBUG = True in your settings, then you get the yellow 'Page Not Found' page.

There are a couple of hardcoded exceptions . When the URL config is empty, or it has not been modified yet, then you get the "Congratulations it worked" page (your example A).

In your example B, you have modified the urlpatterns by adding static() to it. Therefore you get the regular yellow 'Page Not Found' page.

In order for http://localhost:8000/ to work, you need to add a matching URL pattern, for example:

from django.http import HttpResponse
def home_view(request):
    return HttpResponse('home')

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', home_view),
]

Note that putting a view in your urls.py like this is not good practice. I've done it in this case so that you can quickly add a matching pattern for testing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM