{"id":19617740,"url":"https://github.com/macagua/example.django.rest_framework.tutorial","last_synced_at":"2025-06-25T07:38:12.395Z","repository":{"id":43252766,"uuid":"120093397","full_name":"macagua/example.django.rest_framework.tutorial","owner":"macagua","description":"Django Rest Framework 3.9.1 Quickstart Tutorial","archived":false,"fork":false,"pushed_at":"2025-02-26T11:05:51.000Z","size":44,"stargazers_count":3,"open_issues_count":0,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-05T05:33:12.877Z","etag":null,"topics":["django","django-example","django-rest-framework","django-web","examples","examples-python","practice","quickstart-tutorial","tutorial"],"latest_commit_sha":null,"homepage":"https://www.django-rest-framework.org/tutorial/quickstart/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/macagua.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-03T13:26:17.000Z","updated_at":"2025-02-26T11:05:49.000Z","dependencies_parsed_at":"2023-01-25T01:45:41.195Z","dependency_job_id":null,"html_url":"https://github.com/macagua/example.django.rest_framework.tutorial","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/macagua%2Fexample.django.rest_framework.tutorial","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/macagua%2Fexample.django.rest_framework.tutorial/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/macagua%2Fexample.django.rest_framework.tutorial/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/macagua%2Fexample.django.rest_framework.tutorial/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/macagua","download_url":"https://codeload.github.com/macagua/example.django.rest_framework.tutorial/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251238216,"owners_count":21557419,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["django","django-example","django-rest-framework","django-web","examples","examples-python","practice","quickstart-tutorial","tutorial"],"created_at":"2024-11-11T11:07:08.405Z","updated_at":"2025-04-28T02:32:07.223Z","avatar_url":"https://github.com/macagua.png","language":"Python","readme":"# example.django.rest_framework.tutorial\n\nMy practices about the following tutorials:\n\n- [Django 1.9 Project tutorial](https://docs.djangoproject.com/en/1.9/intro/).\n- [Django Rest Framework 3.9.1 tutorial Quickstart](http://www.django-rest-framework.org/tutorial/quickstart/).\n- [Desarrollo de un API REST con Django REST framework, tutorial 1: Serialización](http://jonathanpumares.com/desarrollo-de-un-api-rest-con-django-rest-framework-tutorial-1-serializacion/).\n\n## Installation\n\nThis Django Web app need a lot of Python extras packages, please execute the following command:\n\n```bash\n$ pip install -r requirements.txt --timeout 120\n```\n\n### Build the Django Web app DB\n\n```bash\n$ python manage.py makemigrations\n$ python manage.py migrate\n```\n### Create Django Admin User\n\nThis Django Web app need a to create a Django Admin User, for access and manager the Admin interface, please execute the following command:\n\n**Tip:** for this local installation use the user as **admin** and password as **password123**.\n\n```bash\n$ python manage.py createsuperuser --email admin@mail.com --username admin\nPassword: \nPassword (again): \nSuperuser created successfully.\n```\n\n## Run the Django Web app\n\nYou need to run the Django server, please execute the following command:\n\n```bash\n$ python manage.py runserver\n```\n\n- Open your web browser with the following URL: [http://0.0.0.0:8000/](http://0.0.0.0:8000/) and see the Django Web app.\n\n- Open your web browser with the following URL: [http://0.0.0.0:8000/admin/](http://0.0.0.0:8000/admin/) and see the Django Admin Interface, use the user **admin** and password **password123**.\n\n**Tip:** PLEASE add two **groups**, two **users** and later add a user into a group.\n\n### Quickstart App\n\nFor add data for *Quickstart* App, please access to the following URL: [http://localhost:8000/admin/quickstart/](http://localhost:8000/admin/quickstart/)\n\n### Snippets App\n\nFor add data for *Snippets* App, please access to the following URL: [http://localhost:8000/admin/snippets/](http://localhost:8000/admin/snippets/)\n\n### Testing the API\n\nYou have many APIs Rest for testing, now access to the APIs, both from the command-line, using tools like **curl**, please execute the following command:\n\n#### Users endpoint\n\nFor testing the **users** API Rest, please execute the following command:\n\n```bash\n$ curl -H 'Accept: application/json; indent=4' -u admin:password123 http://127.0.0.1:8000/users/\n[\n    {\n        \"url\": \"http://127.0.0.1:8000/users/4/\",\n        \"username\": \"rocio\",\n        \"email\": \"rociogonzalez@mail.com\",\n        \"groups\": [\n            \"http://127.0.0.1:8000/groups/3/\"\n        ]\n    },\n    {\n        \"url\": \"http://127.0.0.1:8000/users/3/\",\n        \"username\": \"leonardo\",\n        \"email\": \"leonardo@mail.com\",\n        \"groups\": [\n            \"http://127.0.0.1:8000/groups/2/\"\n        ]\n    },\n    {\n        \"url\": \"http://127.0.0.1:8000/users/1/\",\n        \"username\": \"admin\",\n        \"email\": \"admin@mail.com\",\n        \"groups\": []\n    }\n]\n```\n\n#### Groups endpoint\n\nFor testing the **groups** API Rest, please execute the following command:\n\n```bash\n$ curl -H 'Accept: application/json; indent=4' -u admin:password123 http://127.0.0.1:8000/groups/\n[\n    {\n        \"url\": \"http://127.0.0.1:8000/groups/1/\",\n        \"name\": \"Administrators\"\n    },\n    {\n        \"url\": \"http://127.0.0.1:8000/groups/2/\",\n        \"name\": \"Plone\"\n    },\n    {\n        \"url\": \"http://127.0.0.1:8000/groups/3/\",\n        \"name\": \"Botana\"\n    }\n]\n```\n\n#### Events endpoint\n\nFor testing the **events** API Rest, please execute the following command:\n\n```bash\n$ curl -H 'Accept: application/json; indent=4' -u admin:password123 http://127.0.0.1:8000/events/\n[\n    {\n        \"name\": \"New Plone release for 2018\",\n        \"description\": \"Plone will be release a new version at PloneConf 2018\",\n        \"room_number\": 201,\n        \"start_date\": \"2018-02-13T19:42:31Z\",\n        \"finish_date\": \"2018-02-14T19:42:37Z\"\n    },\n    {\n        \"name\": \"New Django release\",\n        \"description\": \"Django will be release a new version at DjangoConf 2018\",\n        \"room_number\": 101,\n        \"start_date\": \"2018-02-12T19:40:59Z\",\n        \"finish_date\": \"2018-02-12T20:41:04Z\"\n    }\n]\n```\n\n#### Blog post endpoint\n\nFor testing the **blog post** API Rest, please execute the following command:\n\n```bash\n$ curl -H 'Accept: application/json; indent=4' -u admin:password123 http://127.0.0.1:8000/blog-post/\n[\n    {\n        \"title\": \"Django project will be release a new version soon\",\n        \"content\": \"Django will be release a new version at DjangoConf 2018\"\n    },\n    {\n        \"title\": \"Plone is Cool\",\n        \"content\": \"Plone is still in fashion with the latest Web technologies\"\n    }\n]\n```\n\n#### Comments endpoint\n\nFor testing the **comments** API Rest, please execute the following command:\n\n```bash\n$ curl -H 'Accept: application/json; indent=4' -u admin:password123 http://127.0.0.1:8000/comments/\n[\n    {\n        \"email\": \"leonardo@mail.com\",\n        \"content\": \"I want to dowload and test it\",\n        \"created\": \"2018-02-03T19:46:43Z\"\n    },\n    {\n        \"email\": \"leonardoc@plone.org\",\n        \"content\": \"#FuckYou Djanguero\",\n        \"created\": \"2018-02-03T19:22:02Z\"\n    },\n    {\n        \"email\": \"djangueroguy@djangoproject.com\",\n        \"content\": \"You are very pathetic you are still using Plone\",\n        \"created\": \"2018-02-03T19:21:33Z\"\n    }\n]\n```\n\n#### Snippets list endpoint\n\nFor testing the **snippets list** API Rest, please execute the following command:\n\n```bash\n$ curl -H 'Accept: application/json; indent=4' -u admin:password123 http://localhost:8000/snippets/list/\n[{\"id\":1,\"title\":\"\",\"code\":\"foo = \\\"bar\\\"\\n\",\"linenos\":false,\"language\":\"python\",\"style\":\"friendly\"},{\"id\":2,\"title\":\"\",\"code\":\"print \\\"hello, world\\\"\\n\",\"linenos\":false,\"language\":\"python\",\"style\":\"friendly\"},{\"id\":3,\"title\":\"\",\"code\":\"print \\\"hello, world\\\"\",\"linenos\":false,\"language\":\"python\",\"style\":\"friendly\"}]\n```\n\n#### Snippets detail endpoint\n\nFor testing the **snippets detail** API Rest, please execute the following command:\n\n```bash\n$ curl -H 'Accept: application/json; indent=4' -u admin:password123 http://localhost:8000/snippets/detail/1/\n{\"id\":1,\"title\":\"\",\"code\":\"foo = \\\"bar\\\"\\n\",\"linenos\":false,\"language\":\"python\",\"style\":\"friendly\"}\n```\n\n## Django Interactive Console\nFor make some practices the Django ORM, please execute the following command:\n\n```bash\n$ python manage.py shell\nPython 2.7.13 (default, Nov 24 2017, 17:33:09) \n[GCC 6.3.0 20170516] on linux2\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n(InteractiveConsole)\n\u003e\u003e\u003e \n```\n\nAt Python Interactive Console, please execute the following command:\n\n```python\n\u003e\u003e\u003e \n\u003e\u003e\u003e # Serializers tutorial section\n\u003e\u003e\u003e \n\u003e\u003e\u003e # Declaring Serializers section\n\u003e\u003e\u003e\n\u003e\u003e\u003e from datetime import datetime\n\u003e\u003e\u003e \n\u003e\u003e\u003e class Comment(object):\n...     def __init__(self, email, content, created=None):\n...         self.email = email\n...         self.content = content\n...         self.created = created or datetime.now()\n... \n\u003e\u003e\u003e comment = Comment(email='leonardo@mail.com', content='THIS IS A TESTING COMMENT')\n\u003e\u003e\u003e comment\n\u003cComment object at 0x7f110aa91510\u003e\n\u003e\u003e\u003e from rest_framework import serializers\n\u003e\u003e\u003e class CommentSerializer(serializers.Serializer):\n...     email = serializers.EmailField()\n...     content = serializers.CharField(max_length=200)\n...     created = serializers.DateTimeField()\n...\n\u003e\u003e\u003e \n\u003e\u003e\u003e # Serializing objects section\n\u003e\u003e\u003e  \n\u003e\u003e\u003e serializer = CommentSerializer(comment)\n\u003e\u003e\u003e serializer\nCommentSerializer(\u003cComment object\u003e):\n    email = EmailField()\n    content = CharField(max_length=200)\n    created = DateTimeField()\n\u003e\u003e\u003e serializer.data\n{'email': u'leonardo@mail.com', 'content': u'THIS IS A TESTING COMMENT', 'created': '2018-02-03T14:51:39.574410'}\n\u003e\u003e\u003e from rest_framework.renderers import JSONRenderer\n\u003e\u003e\u003e json = JSONRenderer().render(serializer.data)\n\u003e\u003e\u003e json\n'{\"email\":\"leonardo@mail.com\",\"content\":\"THIS IS A TESTING COMMENT\",\"created\":\"2018-02-03T14:51:39.574410\"}'\n\u003e\u003e\u003e \n\u003e\u003e\u003e # Deserializing objects section\n\u003e\u003e\u003e \n\u003e\u003e\u003e from django.utils.six import BytesIO\n\u003e\u003e\u003e from rest_framework.parsers import JSONParser\n\u003e\u003e\u003e \n\u003e\u003e\u003e stream = BytesIO(json)\n\u003e\u003e\u003e data = JSONParser().parse(stream)\n\u003e\u003e\u003e data\n{u'content': u'THIS IS A TESTING COMMENT', u'email': u'leonardo@mail.com', u'created': u'2018-02-03T15:16:41.744807'}\n\u003e\u003e\u003e\n\u003e\u003e\u003e serializer = CommentSerializer(data=data)\n\u003e\u003e\u003e serializer.is_valid()\nTrue\n\u003e\u003e\u003e serializer.validated_data\nOrderedDict([(u'email', u'leonardo@mail.com'), (u'content', u'THIS IS A TESTING COMMENT'), (u'created', datetime.datetime(2018, 2, 3, 14, 51, 39, 574410, tzinfo=\u003cdjango.utils.timezone.LocalTimezone object at 0x7f1109752c90\u003e))])\n\u003e\u003e\u003e \n\u003e\u003e\u003e # Saving instances section\n\u003e\u003e\u003e \n\u003e\u003e\u003e class CommentSerializer(serializers.Serializer):\n... \n...     email = serializers.EmailField()\n...     content = serializers.CharField(max_length=200)\n...     created = serializers.DateTimeField()\n...     def create(self, validated_data):\n...         return Comment(**validated_data)\n...     def update(self, instance, validated_data):\n...         instance.email = validated_data.get('email', instance.email)\n...         instance.content = validated_data.get('content', instance.content)\n...         instance.created = validated_data.get('created', instance.created)\n...         return instance\n... \n\u003e\u003e\u003e\n\u003e\u003e\u003e serializer = CommentSerializer(data=data)\n\u003e\u003e\u003e serializer = CommentSerializer(comment, data=data)\n\u003e\u003e\u003e \n\u003e\u003e\u003e # Validation section\n\u003e\u003e\u003e \n\u003e\u003e\u003e serializer = CommentSerializer(data={'email': 'foobar', 'content': 'baz'})\n\u003e\u003e\u003e serializer.is_valid()\nFalse\n\u003e\u003e\u003e serializer.errors\n{'email': [u'Enter a valid email address.'], 'created': [u'This field is required.']}\n\u003e\u003e\u003e \n\u003e\u003e\u003e serializer.is_valid(raise_exception=True)\nTraceback (most recent call last):\n  File \"\u003cconsole\u003e\", line 1, in \u003cmodule\u003e\n  File \"/home/leonardo/virtualenv/django27/local/lib/python2.7/site-packages/rest_framework/serializers.py\", line 245, in is_valid\n    raise ValidationError(self.errors)\nValidationError: {'email': [u'Enter a valid email address.'], 'created': [u'This field is required.']}\n\u003e\u003e\u003e \n\u003e\u003e\u003e # Field-level validation section\n\u003e\u003e\u003e \n\u003e\u003e\u003e from rest_framework import serializers\n\u003e\u003e\u003e class BlogPostSerializer(serializers.Serializer):\n...     title = serializers.CharField(max_length=100)\n...     content = serializers.CharField()\n...     def validate_title(self, value):\n...         \"\"\"\n...         Check that the blog post is about Django.\n...         \"\"\"\n...         if 'django' not in value.lower():\n...             raise serializers.ValidationError(\"Blog post is not about Django\")\n...         return value\n... \n\u003e\u003e\u003e \n\u003e\u003e\u003e # Object-level validation section\n\u003e\u003e\u003e\n\u003e\u003e\u003e class EventSerializer(serializers.Serializer):\n...     description = serializers.CharField(max_length=100)\n...     start = serializers.DateTimeField()\n...     finish = serializers.DateTimeField()\n...     def validate(self, data):\n...         \"\"\"\n...         Check that the start is before the stop.\n...         \"\"\"\n...         if data['start'] \u003e data['finish']:\n...             raise serializers.ValidationError(\"finish must occur after start\")\n...         return data\n... \n\u003e\u003e\u003e \n\u003e\u003e\u003e # Validators section\n\u003e\u003e\u003e\n\u003e\u003e\u003e def multiple_of_ten(value):\n...     if value % 10 != 0:\n...         raise serializers.ValidationError('Not a multiple of ten')\n... \n\u003e\u003e\u003e class GameRecord(serializers.Serializer):\n...     score = serializers.IntegerField(validators=[multiple_of_ten])\n... \n\u003e\u003e\u003e  \n```\n\n# Reference\n\n- [Django 1.9 Project tutorial](https://docs.djangoproject.com/en/1.9/intro/).\n- [Django Rest Framework 3.9.1 tutorial Quickstart](http://www.django-rest-framework.org/tutorial/quickstart/).\n- [Desarrollo de un API REST con Django REST framework, tutorial 1: Serialización](http://jonathanpumares.com/desarrollo-de-un-api-rest-con-django-rest-framework-tutorial-1-serializacion/).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmacagua%2Fexample.django.rest_framework.tutorial","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmacagua%2Fexample.django.rest_framework.tutorial","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmacagua%2Fexample.django.rest_framework.tutorial/lists"}