Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django nested URLs

How do I nest url calls in django? For example, if I have two models defined as

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 the 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 I go to /post/2/comments/1, I am given a Page not found error stating

Using the URLconf defined in advanced_rest.urls, Django tried these URL patterns, in this order:
^post/ ^$
^post/ ^(?P<pk>[0-9]+)/$
^post/ ^(?P<pk>[0-9]+)/comments/$
The current URL, post/2/comments/1, didn't match any of these.

This is not a problem though when I visit /post/2/comments Is this not allowed by django to have nested URL calls like this?

like image 225
user1876508 Avatar asked May 14 '13 18:05

user1876508


1 Answers

I think is probably because you're finishing the regex with the dollar sign $. Try this line without the dollar sign:

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

Hope it helps!

like image 162
Paulo Bu Avatar answered Sep 21 '22 22:09

Paulo Bu