python - Django nested URLs -


how nest url calls in django? example, if have 2 models defined

class post(models.model):     title = models.charfield(max_length=50)     body = models.textfield()     created = models.datetimefield(auto_now_add=true, editable=false)       def __unicode__(self):         return self.title      @property     def comments(self):         return self.comment_set.all()  class comment(models.model):     comment = models.textfield()     post = models.foreignkey(post)     created = models.datetimefield(auto_now_add=true) 

with following url files

root url

urlpatterns = patterns('',     url(r'^post/', include('post.urls')), ) 

post url

urlpatterns = patterns('',     url(r'^$', views.postlist.as_view()),     url(r'^(?p<pk>[0-9]+)/$', views.postdetail.as_view()),     url(r'^(?p<pk>[0-9]+)/comments/$', include('comment.urls')), ) 

comment url

urlpatterns = patterns('',     url(r'^$', commentlist.as_view()),     url(r'^(?p<pk>[0-9]+)/$', commentdetail.as_view()), ) 

but when go /post/2/comments/1, given page not found error stating

using urlconf defined in advanced_rest.urls, django tried these url patterns, in order: ^post/ ^$ ^post/ ^(?p<pk>[0-9]+)/$ ^post/ ^(?p<pk>[0-9]+)/comments/$ current url, post/2/comments/1, didn't match of these. 

this not problem though when visit /post/2/comments not allowed django have nested url calls this?

i think because you're finishing regex dollar sign $. try line without dollar sign:

... url(r'^(?p<pk>[0-9]+)/comments/', include('comment.urls')), ... 

hope helps!


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -