0

I want to use both a slug and id to access my resources, so that the following urls would both point to the same resource

http://site.com/api/resource/this-is-the-slug
http://site.com/api/resource/35

I have added the following to my resource

prepend_urls(self):
    return [
        url(r"^(?P<resource_name>%s)/(?P<slug>[\w\d_.-]+)/$" % self._meta.resource_name, self.wrap_view('dispatch_detail'), name="api_dispatch_detail"),
    ]

Which gets the slug url working fine but unfortunately breaks the id url. How do I get both of them working together?

4

1 回答 1

1

对于同一个视图,您始终可以有 2 个目标此外,蛞蝓从来没有._

prepend_urls(self):
    return [
        url(r"^(?P<resource_name>%s)/(?P<slug>[\w\d-]+)/$" % self._meta.resource_name, self.wrap_view('dispatch_detail'), name="api_dispatch_detail"),
        url(r"^(?P<resource_name>%s)/(?P<id>[\d]+)/$" % self._meta.resource_name, self.wrap_view('dispatch_detail'), name="api_dispatch_detail"),

    ]
于 2013-05-24T16:11:59.880 回答