On Dec 18, 2017 7:19 PM, "Tsuyoshi Takahashi" <go.takaha...@gmail.com>
wrote:

when application urs.py decribed as below

urlpatterns = [
    url('card', views.card),
    url('welcome', views.welcome),
    url('cards', views.cards),
]

Django picks up "url('card', views.card)" when I enter
"http://localhost:8000/firstApp/cards/"; and
"http://localhost:8000/firstApp/card/";.


To me, this is expected and correct behavior.

Django will take the first match, not necessarily the best match. The word
'card' is contained in both 'card' and 'cards', and I believe Django uses
re.match() or a similarly behaving regex parser. Since your 'card' URL is
defined first, it matches. As you've shown, changing the order produces the
desired result.

The real fix is to properly terminate your URL regular expressions to match
as you desire so that the order doesn't matter. Try defining your URL's
like this:

urlpatterns = [
    url(r'card/$', views.card),
    url(r'welcome/$', views.welcome),
    url(r'cards/$', views.cards),
]


The extra /$ at the end indicates that the URL will end with a /. That
should be enough to differentiate the requests.

See the URL dispatcher documentation:

https://docs.djangoproject.com/en/2.0/topics/http/urls/

If you aren't familiar with regular expressions, also see the Python re
module documentation:

https://docs.python.org/3.4/library/re.html

-James

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2Be%2BciVP-VtBNEhMQT%3DZnO35BK7BSo_rNSJ1xUvRaesZHuQknQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to