Anonymous user
Django for Designers: Difference between revisions
→create blank api.py file and add it to urls.py: Add Tastypie sketch
imported>Paulproteus (→Templates and links for login/logout/etc: Remove use of 'touch', which won't work for Windows users) |
imported>Paulproteus (→create blank api.py file and add it to urls.py: Add Tastypie sketch) |
||
Line 2,435:
===== create blank api.py file and add it to urls.py =====
Within ''bookmarks'', create a new file called ''api.py''. Let the contents of that file be:
<source lang="python">
from tastypie.resources import ModelResource
from bookmark.models import Bookmark
class BookmarkResource(ModelResource):
* git commit▼
class Meta:
queryset = Bookmark.objects.all()
resource_name = 'bookmark'
</source>
For this file to be invoked in request processing, we have to tie it into a url. So let's edit ''urls.py''. First, add a new import to the top of the file:
<source lang="python">
from bookmarks.api import BookmarkResource
</source>
Below the imports, add this on a line of its own:
<source lang="python">
bookmark_resource = BookmarkResource()
</source>
Within the urlpatterns sequence, add this on a line of its own:
<source lang="python">
(r'^api/', include(bookmark_resource.urls)),
</source>
Now your API should be live and on the web! Visit http://127.0.0.1:8000/api/bookmark/?format=json and you should see a machine-readable list of all the bookmarks on your site!
With all that done and working, now is a great time to commit. Run:
<source lang="bash">
# in django-for-designers
</source>
and make sure you ''git add'' any new files. With that addressed, run:
<source lang="bash">
# in django-for-designers
$ git commit -a -m 'Added an API with Tastypie'
</source>
===== Add our api method to api.py =====
|