Django nested URLs

只愿长相守 提交于 2021-02-19 01:04:15

问题


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?


回答1:


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!




回答2:


You have a $ at the end of r'^(?P<pk>[0-9]+)/comments/$'.

That means Django will only match with that URL when there is nothing after that.

So any longer URLs currently won't be considered. Therefore, you need to update the regular expression to:

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



回答3:


Above Django 2.0 you can use simply...

urlpatterns = [
    path('<pk>/comments/', include('comment.urls')),
]


来源:https://stackoverflow.com/questions/16550568/django-nested-urls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!