In the test example http://django-rest-interface.googlecode.com/svn/trunk/django_restapi_tests/examples/custom_urls.py on line 19 to they parse the request.path to get the poll_id. This looks very fragile to me. If the url changes then this line breaks. I have attempted to pass in the poll_id but this did not work. So my question is how do I use the poll_id (or any other value) gathered from the url?
A:
Views are only called when the associated url is matched. By crafting the url regex properly, you can guarantee that any request passed to your view will have the poll_id
at the correct position in the request path. This is what the example does:
url(r'^json/polls/(?P<poll_id>\d+)/choices/$', json_choice_resource, {'is_entry':False}),
The json_choice_resource
view is an instance of django_restapi.model_resource.Collection
and thus the read()
method of Collection
will only ever act on requests with paths of the expected format.
vezult
2009-04-24 15:34:02
So if I understandyou correctly the split only works on the portion of the url that is in the calling url.py file. <br>So if the url is http://SomeHost.com/json/polls/5/choices/ or http://SomeHost.com/SomeSystem/json/polls/5/choices/ the split("/") will resolve the same because the host portion of the url is in another url.py file?
Herr Janvier
2009-04-24 16:12:16
That is correct. If you want the full path relative to the domain, you need to use request.get_full_path()
vezult
2009-04-24 17:06:14