Saurabh Kumar Karn
Saurabh Kumar Karn

Reputation: 111

django url regular expression repeats in URL

If I create URL

urlpatterns = patterns('',     
     url(r'^admin/', include(admin.site.urls)),
     url(r'^about/',views.about , name = 'about'),
     url(r'^rango/',views.index, name = 'index'),
)

the link 127.0.0.1:8000/about and 127.0.0.1:8000/about/about/ will direct you to same page isn't it? How do I stop that ? I only want

domain-name/about/  

to be valid and anything (/about/about/about/...) should be invalid page.

Upvotes: 1

Views: 101

Answers (2)

knbk
knbk

Reputation: 53659

More generally, about/anything/ will match as well. The $ indicates the end of a string (or in this case url), just like ^ indicates the start of it. Checking against r'^about/' only checks that the url starts with about/, not where it ends.

That is generally the behaviour you want when you include another url config, like admin/. In that case, the pattern shouldn't end after admin/, but there should be another part that matches a configured url in the included file. But if your pattern is a single view, you generally want the url to end after the end of the pattern, that's when you include the $, e.g. r'^about/$'.

Upvotes: 0

ssskip
ssskip

Reputation: 259

add $ to the end

url(r'^about/$',views.about , name = 'about'),

Upvotes: 2

Related Questions