{"id":7771,"date":"2015-10-24T15:31:06","date_gmt":"2015-10-24T15:31:06","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/10\/24\/tomchristie-django-rest-framework\/"},"modified":"2015-10-24T15:31:06","modified_gmt":"2015-10-24T15:31:06","slug":"tomchristie-django-rest-framework","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/10\/24\/tomchristie-django-rest-framework\/","title":{"rendered":"tomchristie\/django-rest-framework"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/secure.travis-ci.org\/tomchristie\/django-rest-framework.svg?branch=master\" \/> <img decoding=\"async\" src=\"http:\/\/img.shields.io\/pypi\/v\/djangorestframework.svg\" \/><\/p>\n<p><strong>Awesome web-browsable Web APIs.<\/strong><\/p>\n<p>Full documentation for the project is available at http:\/\/www.django-rest-framework.org.<\/p>\n<p><strong>Note<\/strong>: We have now released Django REST framework 3.1. For older codebases you may want to refer to the version 2.4.4 source code, and documentation.<\/p>\n<p>For more details see the 3.1 release notes<\/p>\n<h2>Overview<\/h2>\n<p>Django REST framework is a powerful and flexible toolkit for building Web APIs.<\/p>\n<p>Some reasons you might want to use REST framework:<\/p>\n<p>There is a live example API for testing purposes, available here.<\/p>\n<p><strong>Below<\/strong>: <em>Screenshot from the browsable API<\/em><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.django-rest-framework.org\/img\/quickstart.png\" \/><\/p>\n<h2>Requirements<\/h2>\n<ul>\n<li>Python (2.6.5+, 2.7, 3.2, 3.3, 3.4)<\/li>\n<li>Django (1.4.11+, 1.5.6+, 1.6.3+, 1.7, 1.8)<\/li>\n<\/ul>\n<p>Install using <code>pip<\/code>\u2026<\/p>\n<pre><code>pip install djangorestframework\n<\/code><\/pre>\n<p>Add <code>'rest_framework'<\/code> to your <code>INSTALLED_APPS<\/code> setting.<\/p>\n<pre><code>INSTALLED_APPS = (\n    ...\n    'rest_framework',\n)\n<\/code><\/pre>\n<h2>Example<\/h2>\n<p>Let\u2019s take a look at a quick example of using REST framework to build a simple model-backed API for accessing users and groups.<\/p>\n<p>Startup up a new project like so\u2026<\/p>\n<pre><code>pip install django\npip install djangorestframework\ndjango-admin.py startproject example .\n.\/manage.py syncdb\n<\/code><\/pre>\n<p>Now edit the <code>example\/urls.py<\/code> module in your project:<\/p>\n<pre><code>from django.conf.urls import url, include\nfrom django.contrib.auth.models import User\nfrom rest_framework import serializers, viewsets, routers\n\n# Serializers define the API representation.\nclass UserSerializer(serializers.HyperlinkedModelSerializer):\n    class Meta:\n        model = User\n        fields = ('url', 'username', 'email', 'is_staff')\n\n\n# ViewSets define the view behavior.\nclass UserViewSet(viewsets.ModelViewSet):\n    queryset = User.objects.all()\n    serializer_class = UserSerializer\n\n\n# Routers provide a way of automatically determining the URL conf.\nrouter = routers.DefaultRouter()\nrouter.register(r'users', UserViewSet)\n\n\n# Wire up our API using automatic URL routing.\n# Additionally, we include login URLs for the browsable API.\nurlpatterns = [\n    url(r'^', include(router.urls)),\n    url(r'^api-auth\/', include('rest_framework.urls', namespace='rest_framework'))\n]\n<\/code><\/pre>\n<p>We\u2019d also like to configure a couple of settings for our API.<\/p>\n<p>Add the following to your <code>settings.py<\/code> module:<\/p>\n<pre><code>INSTALLED_APPS = (\n    ...  # Make sure to include the default installed apps here.\n    'rest_framework',\n)\n\nREST_FRAMEWORK = {\n    # Use Django's standard `django.contrib.auth` permissions,\n    # or allow read-only access for unauthenticated users.\n    'DEFAULT_PERMISSION_CLASSES': [\n        'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'\n    ]\n}\n<\/code><\/pre>\n<p>That\u2019s it, we\u2019re done!<\/p>\n<pre><code>.\/manage.py runserver\n<\/code><\/pre>\n<p>You can now open the API in your browser at <code>http:\/\/127.0.0.1:8000\/<\/code>, and view your new \u2018users\u2019 API. If you use the <code>Login<\/code> control in the top right corner you\u2019ll also be able to add, create and delete users from the system.<\/p>\n<p>You can also interact with the API using command line tools such as <code>curl<\/code>. For example, to list the users endpoint:<\/p>\n<pre><code>$ curl -H 'Accept: application\/json; indent=4' -u admin:password http:\/\/127.0.0.1:8000\/users\/\n[\n    {\n        \"url\": \"http:\/\/127.0.0.1:8000\/users\/1\/\",\n        \"username\": \"admin\",\n        \"email\": \"admin@example.com\",\n        \"is_staff\": true,\n    }\n]\n<\/code><\/pre>\n<p>Or to create a new user:<\/p>\n<pre><code>$ curl -X POST -d username=new -d email=new@example.com -d is_staff=false -H 'Accept: application\/json; indent=4' -u admin:password http:\/\/127.0.0.1:8000\/users\/\n{\n    \"url\": \"http:\/\/127.0.0.1:8000\/users\/2\/\",\n    \"username\": \"new\",\n    \"email\": \"new@example.com\",\n    \"is_staff\": false,\n}\n<\/code><\/pre>\n<h2>Documentation &amp; Support<\/h2>\n<p>Full documentation for the project is available at http:\/\/www.django-rest-framework.org.<\/p>\n<p>For questions and support, use the REST framework discussion group, or <code>#restframework<\/code> on freenode IRC.<\/p>\n<p>You may also want to follow the author on Twitter.<\/p>\n<h2>Security<\/h2>\n<p>If you believe you\u2019ve found something in Django REST framework which has security implications, please <strong>do not raise the issue in a public forum<\/strong>.<\/p>\n<p>Send a description of the issue via email to rest-framework-security@googlegroups.com. The project maintainers will then work with you to resolve any issues where required, prior to any public disclosure.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Awesome web-browsable Web APIs. Full documentation for the project is available at http:\/\/www.django-rest-framework.org. Note: We have now released Django REST framework 3.1. For older codebases you may want to refer to the version 2.4.4 source code, and documentation. For more details see the 3.1 release notes Overview Django REST framework is a powerful and flexible [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7771","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7771","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7771"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7771\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}