Jump to content

Django for Designers: Difference between revisions

imported>Paulproteus
(→‎Templates and links for login/logout/etc: Remove use of 'touch', which won't work for Windows users)
imported>Paulproteus
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:
* Create api.py, with a basically blank Resource subclass
 
<source lang="python">
* Add relevant stuff to urls.py
from tastypie.resources import ModelResource
from bookmark.models import Bookmark
 
* Test that viewing our app at the API URL doesn't crash
 
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
*$ git commitstatus
</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 =====
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.