Added resourceexample, moved simpleexample to modelresourceexample

This commit is contained in:
tom christie tom@tomchristie.com 2011-02-01 22:37:51 +00:00
parent 196c21f376
commit 6ce5b643fb
10 changed files with 77 additions and 9 deletions

View File

@ -8,4 +8,19 @@
{% block htmltitle %}<title>{% if pagename == 'index' %}Django REST framework{% else %}{{ titleprefix }}{{ title|striptags|e }}{% endif %}</title>{% endblock %}
{% block extrahead %}
{{ super() }}
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-18852272-2']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
{% endblock %}

View File

@ -19,5 +19,5 @@ class MyModel(models.Model):
@models.permalink
def get_absolute_url(self):
return ('simpleexample.views.MyModelResource', (self.pk,))
return ('modelresourceexample.views.MyModelResource', (self.pk,))

View File

@ -1,6 +1,6 @@
from django.conf.urls.defaults import patterns, url
urlpatterns = patterns('simpleexample.views',
urlpatterns = patterns('modelresourceexample.views',
url(r'^$', 'MyModelRootResource'),
url(r'^([0-9]+)/$', 'MyModelResource'),
)

View File

@ -1,5 +1,5 @@
from djangorestframework.modelresource import ModelResource, RootModelResource
from simpleexample.models import MyModel
from modelresourceexample.models import MyModel
FIELDS = ('foo', 'bar', 'baz', 'absolute_url')

View File

View File

@ -0,0 +1,6 @@
from django.conf.urls.defaults import patterns, url
urlpatterns = patterns('resourceexample.views',
url(r'^$', 'ExampleResource'),
url(r'^(?P<num>[0-9]+)/$', 'AnotherExampleResource'),
)

View File

@ -0,0 +1,35 @@
from django import forms
from djangorestframework.resource import Resource
from djangorestframework.response import Response, status
class MyForm(forms.Form):
foo = forms.BooleanField()
bar = forms.IntegerField(help_text='Must be an integer.')
baz = forms.CharField(max_length=32, help_text='Free text. Max length 32 chars.')
class ExampleResource(Resource):
"""A basic read only resource that points to 3 other resources."""
allowed_methods = anon_allowed_methods = ('GET',)
def get(self, request, auth):
return {"Some other resources": [self.reverse(AnotherExampleResource, num=num) for num in range(3)]}
class AnotherExampleResource(Resource):
"""A basic GET-able/POST-able resource."""
allowed_methods = anon_allowed_methods = ('GET', 'POST')
form = MyForm # Optional form validation on input
def get(self, request, auth, num):
"""Handle GET requests"""
if int(num) > 2:
return Response(status.HTTP_404_NOT_FOUND)
return "GET request to AnotherExampleResource %s" % num
def post(self, request, auth, content, num):
"""Handle POST requests"""
if int(num) > 2:
return Response(status.HTTP_404_NOT_FOUND)
return "POST request to AnotherExampleResource %s, with content: %s" % (num, repr(content))

View File

@ -95,7 +95,9 @@ INSTALLED_APPS = (
'django.contrib.messages',
#'django.contrib.admin',
'djangorestframework',
'simpleexample',
'resourceexample',
'modelresourceexample',
'objectstore',
'pygments_api',
'blogpost',

View File

@ -5,18 +5,28 @@ from djangorestframework.resource import Resource
#admin.autodiscover()
class RootResource(Resource):
"""This is the sandbox for the examples provided with django-rest-framework.
These examples are here to help you get a better idea of the some of the
features of django-rest-framework API, such as automatic form and model validation,
support for multiple input and output media types, etc...
Please feel free to browse, create, edit and delete the resources here, either
in the browser, from the command line, or programmatically."""
allowed_methods = anon_allowed_methods = ('GET',)
def get(self, request, auth):
return {'simple example': self.reverse('simpleexample.views.MyModelRootResource'),
'pygments example': self.reverse('pygments_api.views.PygmentsRoot'),
'object store example': self.reverse('objectstore.views.ObjectStoreRoot'),
'blog post example': self.reverse('blogpost.views.BlogPostRoot'),}
return {'Simple Resource example': self.reverse('resourceexample.views.ExampleResource'),
'Simple ModelResource example': self.reverse('modelresourceexample.views.MyModelRootResource'),
'Object store API (Resource)': self.reverse('objectstore.views.ObjectStoreRoot'),
'A pygments pastebin API (Resource + forms)': self.reverse('pygments_api.views.PygmentsRoot'),
'Blog posts API (ModelResource)': self.reverse('blogpost.views.BlogPostRoot'),}
urlpatterns = patterns('',
(r'^$', RootResource),
(r'^simple-example/', include('simpleexample.urls')),
(r'^model-resource-example/', include('modelresourceexample.urls')),
(r'^resource-example/', include('resourceexample.urls')),
(r'^object-store/', include('objectstore.urls')),
(r'^pygments/', include('pygments_api.urls')),
(r'^blog-post/', include('blogpost.urls')),