{"id":18768368,"url":"https://github.com/mynameiscarsten/django-rest","last_synced_at":"2026-05-04T13:31:38.734Z","repository":{"id":194057334,"uuid":"689950358","full_name":"MyNameIsCarsten/django-rest","owner":"MyNameIsCarsten","description":"REST API built with Django REST framework.","archived":false,"fork":false,"pushed_at":"2023-09-12T21:00:00.000Z","size":166,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-20T23:16:56.054Z","etag":null,"topics":["class-based-views","django","django-rest-framework","forms","rest-api"],"latest_commit_sha":null,"homepage":"","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/MyNameIsCarsten.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-09-11T08:41:54.000Z","updated_at":"2023-09-12T06:19:43.000Z","dependencies_parsed_at":"2024-11-07T19:12:19.996Z","dependency_job_id":"53ed884f-d589-4259-bb7d-c3c50bb89279","html_url":"https://github.com/MyNameIsCarsten/django-rest","commit_stats":null,"previous_names":["mynameiscarsten/django-rest"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/MyNameIsCarsten/django-rest","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MyNameIsCarsten%2Fdjango-rest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MyNameIsCarsten%2Fdjango-rest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MyNameIsCarsten%2Fdjango-rest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MyNameIsCarsten%2Fdjango-rest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MyNameIsCarsten","download_url":"https://codeload.github.com/MyNameIsCarsten/django-rest/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MyNameIsCarsten%2Fdjango-rest/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32609406,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-04T10:08:07.713Z","status":"ssl_error","status_checked_at":"2026-05-04T10:08:02.005Z","response_time":58,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["class-based-views","django","django-rest-framework","forms","rest-api"],"created_at":"2024-11-07T19:12:14.271Z","updated_at":"2026-05-04T13:31:38.715Z","avatar_url":"https://github.com/MyNameIsCarsten.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# REST API built with Django REST framework\n\nFor this app, we combine **Django** (a free, open source framework based on Python) with an **REST API** (representational state transfer API).\n\nThe combination takes places through the **Django REST framework** (DRF) toolkit.\n\nSince Django utilizes a **Model-View-Template** (MVT) architectural pattern, the toolkit is able to use the **class-based views**.\n\nThe outcome is a fully functional **CRUD** Django REST API.\n\n# Endpoints\nEndpoints define the structure and usage for the GET, POST, PUT, and DELETE HTTP methods within an RESTful API.\n\nThis app uses two endpoints:\n|Endpoint|GET|POST|PUT|DELETE|View|\n|:--:|:--:|:--:|:--:|:--:|:--:|\n|``todos/api``|1. List All: List all to-dos for requested user|2. Create: Add a new to-do|N/A|N/A|``TodoListApiView``|\n|``todos/api/\u003cint:todo_id\u003e``|3. Retrieve: Get a to-do with given ``todo_id``|N/A|4. Update: Update a to-do with given ``todo_id``|5. Delete: Delete a to-do with given ``todo_id``|``TodoDetailApiView``|\n\nThese endpoints can simply be accessed via their specific urls e.g.:\n`http://127.0.0.1:8000/todos/api/`\n\n![List](list-api.jpg)\n\n`http://127.0.0.1:8000/todos/api/2`\n\n![List](detail-api.jpg)\n\n\nHowever, this code also feaures the possibility to submit a new entry via a form submission.\n\nThe form is located here: `http://127.0.0.1:8000/`\n\nThe associated view is `contact` which renders a from for the `todo` model and checks if it is valid.\n\n![Form](todo-form.jpg)\n\nThe `@login_required` decorators ensures that only a logged in user can access the form. The logged in user is then automatically associated with the user's submitted task.\n\n# Model serializer\nThe conversion of an Model object into an API-usable format (e.g. JSON) is done via a serializer. The Django REST framework does this via the ModelSerializer class (see `serializers.py`).\n\n# Using the Fetch Api with Django Rest Framework\nFor the purpose of demonstrating the usage of the fetch API in connection with our endpoints, I have integrated a JavaScript (`script.js`).\n\n## Acquire csrf token\nFirst we need to acquire our csrf token (as decribed in the [documentaion](https://docs.djangoproject.com/en/4.2/howto/csrf/#acquiring-the-token-if-csrf-use-sessions-and-csrf-cookie-httponly-are-false)):\n```\n// Acquiring the csrf token\nfunction getCookie(name) {\n    let cookieValue = null;\n    if (document.cookie \u0026\u0026 document.cookie !== '') {\n        const cookies = document.cookie.split(';');\n        for (let i = 0; i \u003c cookies.length; i++) {\n            const cookie = cookies[i].trim();\n            // Does this cookie string begin with the name we want?\n            if (cookie.substring(0, name.length + 1) === (name + '=')) {\n                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));\n                break;\n            }\n        }\n    }\n    return cookieValue;\n}\nconst csrftoken = getCookie('csrftoken');\n```\n## Fetch data from api using JavaScript\nAfterwards we fetch data from our api endpoint:\n```\n// Example of fetching data from our api\nfetch(\"/todos/api/1/\", {\n    method: \"get\",\n    headers: {\n        \"X-CSRFToken\": getCookie(\"csrftoken\"),\n        \"Accept\": \"application/json\",\n        \"Content-Type\": \"application/json\"\n    },\n}).then(function(response) {\n    return response.json();\n}).then(function(data) {\n    console.log(\"Data is ok\", data); // log data to console\n}).catch(function(ex) {\n    console.log(\"parsing failed\", ex); // log error to console\n});\n```\n\nSince I have implemented the script in the `contact.html` header, we get the following log within our console if we visit `http://127.0.0.1:8000/`:\n\n![Fetch Api](./fetch-api.jpg)\n\nIf we compare this to what we get with our api endpoint (`http://127.0.0.1:8000/todos/api/1/`) we see that they are identical:\n\n![Detail Api](./api-detail-1.jpg)\n\n\n# Source\nThe code is based on tutorials: \n- [How to create a REST API with Django REST framework](https://blog.logrocket.com/django-rest-framework-create-api/)\n- [Django And Fetch API Form Submissions –Without Page Reloading](https://ridwanray.medium.com/django-and-fetch-api-form-submissions-without-page-reloading-dc5106598005)\n- [Using the Fetch Api with Django Rest Framework](https://gist.github.com/marteinn/3785ff3c1a3745ae955c)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmynameiscarsten%2Fdjango-rest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmynameiscarsten%2Fdjango-rest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmynameiscarsten%2Fdjango-rest/lists"}