URL Mapping Confusion

62 views
Skip to first unread message

Chris Kavanagh

unread,
Sep 9, 2013, 6:30:28 PM9/9/13
to Digest Recipients
I haven't used this group in a long time, so I hope I do everything correctly. If I don't, I apologize in advance.

I thought I understand the way django uses URL's in the urlconf to match URL's, but I was wrong. Here's my question:

Here's my urlconf. . .
    url(r'^all/$', 'article.views.articles'),
    url(r'^get/(?P<article_id>\d+)/$', 'article.views.article'),
    url(r'^language/(?P<language>[a-z\-]+)/$', 'article.views.language'),
    url(r'^create/$', 'article.views.create'),)

and here's the other one in the site directory.
urlpatterns = patterns('',

    (r'^articles/', include('article.urls')),
    url(r'^accounts/login/$', 'mysite.views.login'),
    url(r'^accounts/auth/$', 'mysite.views.auth_view'),
    url(r'^accounts/logout/$', 'mysite.views.logout'),
    url(r'^accounts/loggedin/$', 'mysite.views.loggedin'),
    url(r'^accounts/invalid/$', 'mysite.views.invalid_login'),
    url(r'^accounts/register/$', 'mysite.views.register_user'),
    url(r'^accounts/register_success/$', 'mysite.views.register_success'),)

Here's the question: If I have the URL "http://127.0.0.1:8000/articles/create/" it works fine and takes me to the correct page. But why does it work when I have the "/article" in the url. According to the urlconf last entry r'^create/$', it should only match a url with "/create" NOT a url with "/article/create". The caret means "starts with" and the $ means "end with", so to get to the "create" page it should be a url with just "/create/" in it, right?

I've looked at the django docs explanation, I've gone through every web site that talks about this, I understand regex, but I still don't understand why this is happening.

Any help is GREATLY appreciated.

Thanks,
Chris Kavanagh

Thomas Lockhart

unread,
Sep 9, 2013, 6:41:23 PM9/9/13
to django...@googlegroups.com
I'm a bit confused by your question. All of your "urlconf" paths will
need to have "/articles" in the path to be found. Although regex does
use "^" and "$" to denote beginning and end, you have split your pattern
matching into two stages so they are just matching the pattern available
in each stage.

Was your mention of "/article" above a typo and you meant "/articles"?

You might want to follow up with a description of what behavior you need
to help folks give you a solution.

hth

- Tom

Chris Kavanagh

unread,
Sep 9, 2013, 7:13:13 PM9/9/13
to django...@googlegroups.com
Tom, thanks so much for the reply. . .Yes, that was a typo, it should've been "/articles" not "/article".
 I know this is going to be a stupid question, but why do my "urlconf" paths need to have "/articles"
in it as you said?

Apparently I've missed something in my understanding of how all this works. Thanks again for the help!

Thomas Lockhart

unread,
Sep 9, 2013, 7:48:53 PM9/9/13
to django...@googlegroups.com

Tom, thanks so much for the reply. . .Yes, that was a typo, it should've been "/articles" not "/article".
 I know this is going to be a stupid question, but why do my "urlconf" paths need to have "/articles"
in it as you said?
There are three steps in the interpretation of the pattern.
1) If the pattern starts with "/articles", try matching within your "urlconf" file.
2) If the remaining portion of the url matches a pattern in the "urlconf" file, stop there.
3) If the remaining portion does not match in the "urlconf" file, keep looking farther down your main url file.

If you don't want the /articles prefix in your paths, write your patterns more explicitly at the top level.

It *is* pretty typical to have separate apps have distinct prefixes in their URL paths.

hth

                              - Tom

Chris Kavanagh

unread,
Sep 9, 2013, 8:40:03 PM9/9/13
to Digest Recipients
This is just a tutorial I'm going through Tom. I'm not trying to write anything, just trying to understand this part. And I apologize for making my question(s) confusing, I'm not exactly sure how to ask it.

Let's back up for a second. I'm not trying to match anything, I was telling you what I already had in my files (from the tutorial). What I do not understand is why the "/articles/" part of the url needs be included when I want to go to my  "/create/" view? In other words, why does the url need to be "http://127.0.0.1:8000/articles/create" INSTEAD OF "http://127.0.0.1:8000/create" ? Am I making sense, lol?

I have "url(r'^create/$', 'article.views.create')" in my urlconf, so why must I include "/article/" when I want to go to "http://127.0.0.1:8000/create"? You said "All of your "urlconf" paths will need to have "/articles" in the path to be found". Why? That is my question? I'm obviously not understanding something that I should already know. I get that I also have "(r'^articles/', include('article.urls'))" in my main urlconf. I thought this line of code just linked up the other urlconf in my app package and that's all it did.

I hope I made my question a little clearer, lol. I'm not sure how to ask what I want to know. But again, THANKS for the help Tom!



--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/43W4LSOuUUw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

Thomas Lockhart

unread,
Sep 9, 2013, 10:57:51 PM9/9/13
to django...@googlegroups.com
On 9/9/13 5:40 PM, Chris Kavanagh wrote:
This is just a tutorial I'm going through Tom. I'm not trying to write anything, just trying to understand this part. And I apologize for making my question(s) confusing, I'm not exactly sure how to ask it.

Let's back up for a second. I'm not trying to match anything, I was telling you what I already had in my files (from the tutorial). What I do not understand is why the "/articles/" part of the url needs be included when I want to go to my  "/create/" view? In other words, why does the url need to be "http://127.0.0.1:8000/articles/create" INSTEAD OF "http://127.0.0.1:8000/create" ? Am I making sense, lol?

I have "url(r'^create/$', 'article.views.create')" in my urlconf, so why must I include "/article/" when I want to go to "http://127.0.0.1:8000/create"? You said "All of your "urlconf" paths will need to have "/articles" in the path to be found". Why? That is my question? I'm obviously not understanding something that I should already know. I get that I also have "(r'^articles/', include('article.urls'))" in my main urlconf. I thought this line of code just linked up the other urlconf in my app package and that's all it did.
Well, no it is not "just linked up". Since you put a pattern "^/articles/$" to be matched in the primary urls.py file, and since the next argument is "include('article.urls')", that secondary "article.urls" will never be seen to be matched unless you get a match on the leading "/articles/". Go back to my previous response and look at the three steps in matching a nested URL.

hth

                             - Tom



I hope I made my question a little clearer, lol. I'm not sure how to ask what I want to know. But again, THANKS for the help Tom!



On Mon, Sep 9, 2013 at 7:48 PM, Thomas Lockhart <tlockh...@gmail.com> wrote:

Tom, thanks so much for the reply. . .Yes, that was a typo, it should've been "/articles" not "/article".
 I know this is going to be a stupid question, but why do my "urlconf" paths need to have "/articles"
in it as you said?
There are three steps in the interpretation of the pattern.
1) If the pattern starts with "/articles", try matching within your "urlconf" file.
2) If the remaining portion of the url matches a pattern in the "urlconf" file, stop there.
3) If the remaining portion does not match in the "urlconf" file, keep looking farther down your main url file.

If you don't want the /articles prefix in your paths, write your patterns more explicitly at the top level.

It *is* pretty typical to have separate apps have distinct prefixes in their URL paths.

hth

                              - Tom
--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/43W4LSOuUUw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

--
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...@googlegroups.com.

Chris Kavanagh

unread,
Sep 10, 2013, 11:54:40 AM9/10/13
to django...@googlegroups.com
I understand it now Tom, thanks to you. I didn't quite get the "include" statement and what it did until you mentioned it, and a little help from the django book. . .Again, THANKS for taking the time to help, it's greatly appreciated!

Chris
Reply all
Reply to author
Forward
0 new messages