This project is a collection of various APIs for Django projects, including functionalities such as authentication, product management for e-commerce, token management, and more. It is designed to provide developers with ready-to-use API implementations for common Django tasks, all powered by Django Rest Framework (DRF).
The project consists of several sub-projects, each dedicated to a specific area of functionality. Each sub-project contains a set of APIs implemented using Django Rest Framework.
.
├── 01-Auth
│ ├── 01-token_based_auth
│ ├── 02-API throttling for class-based views
│ ├── 03-Djoser library
│ └── 04-JWT(JSON Web Token)
├── 02-Shop API
│ ├── 01-Simple API
│ ├── 02-Simple DRF
│ ├── 03-ViewSet
│ ├── 04-ModelViewSet (ReadOnlyModelViewSet)
│ ├── 05-Generic views
│ ├── 06-Serialization
│ ├── 07-Deserialization
│ ├── 08-FilteringSearchingOrdering
│ ├── 09-Validation
│ ├── 10-DataSanitization
│ ├── 11-Pagination
└── 12-ClassBasePaginationFiltering
- Python 3.x
- Django 3.x or later
- Django Rest Framework (DRF)
git clone https://github.com/mhdemd/Django-REST-APIs-Reference.git
cd Django-REST-APIs-Reference/path/to/your/application
python -m venv venv
source venv/bin/activate # On Windows use `venv\Scripts\activate`
pip install -r requirements.txt
python manage.py migrate
python manage.py runserver
Once the server is running, you can access the APIs at the following endpoints (example):
POST /api/auth/token/
- Obtain a token for authenticationGET /api/products/
- List all productsPOST /api/orders/
- Create a new order
The APIs support multiple authentication methods, including:
- Token-based authentication: This method involves generating a token upon successful login and using that token for subsequent requests to access protected resources.
- Session-based authentication: This method maintains a user session on the server, allowing authenticated users to make requests as long as the session is active.
- JWT-based authentication: JSON Web Tokens (JWT) are used to securely transmit user authentication data, which can be verified without needing to store session data on the server.
For more details on how each authentication method is implemented and examples of their usage, refer to the table below.
Note: Further information on additional APIs and their functionalities is provided in the table below.
API's list | |||||||
---|---|---|---|---|---|---|---|
No | Folder | Sub Folder | URLs | Method | Form Data | Auth Type | Function |
1 | 01-Auth | 01-token_based_auth | /api/secret/ | GET | - | OAuth 2.0 (Token + Header Prefix=Token) | Access to specific view with token |
2 | 01-Auth | 01-token_based_auth | /api/api-token-auth/ | POST | username - password | - | Get token for the specified user |
3 | 01-Auth | 01-token_based_auth | /api/manager-view/ | GET | - | OAuth 2.0 (Token + Header Prefix=Token) | Access to manager group view |
4 | 01-Auth | 01-token_based_auth | /api/throttle-check/ | GET | - | - | Limit the number of anonymous requests |
5 | 01-Auth | 01-token_based_auth | /api/throttle-check-auth/ | GET | - | OAuth 2.0 (Token + Header Prefix=Token) | Limit the number of authenticated requests |
6 | 01-Auth | 02-API throttling for class-based views | /api/register/ | GET | username - password | - | Limit the number of authenticated requests for class-based views |
7.1 | 01-Auth | 03-Djoser library | /auth/users/ | POST | email - password - re_password | - | Register new user |
7.2 | 01-Auth | 03-Djoser library | /auth/users/ | GET | - | OAuth 2.0 (Token + Header Prefix=Token) | Get complete details of user(s) |
8 | 01-Auth | 03-Djoser library | /auth/token/login/ | POST | email - password | - | Create user token |
9 | 01-Auth | 03-Djoser library | /auth/token/logout/ | POST | - | OAuth 2.0 (Token + Header Prefix=Token) | Destroy user token |
10 | 01-Auth | 03-Djoser library | /auth/users/me/ | GET | - | OAuth 2.0 (Token + Header Prefix=Token) | Get complete details of the user |
11 | 01-Auth | 03-Djoser library | /auth/users/me/ | PATCH | Fields to be modified | OAuth 2.0 (Token + Header Prefix=Token) | Update some user details |
12 | 01-Auth | 03-Djoser library | /auth/users/me/ | PUT | All fields must be provided with new values | OAuth 2.0 (Token + Header Prefix=Token) | Update all user details |
13 | 01-Auth | 03-Djoser library | /auth/users/me/ | DELETE | current_password | OAuth 2.0 (Token + Header Prefix=Token) | Delete user |
14 | 01-Auth | 03-Djoser library | /auth/users/set_email/ | POST | new_email - current_password | OAuth 2.0 (Token + Header Prefix=Token) | Change user email |
15 | 01-Auth | 03-Djoser library | /auth/users/set_password/ | POST | new_password - re_new_password - current_password | OAuth 2.0 (Token + Header Prefix=Token) | Directly change the user's password |
16 | 01-Auth | 03-Djoser library | /auth/users/reset_password/ | POST | - | Send password reset email | |
17 | 01-Auth | 03-Djoser library | /auth/users/reset_password_confirm/ | POST | uid - token - new_password - re_new_password | - | Confirm password reset via email |
18 | 01-Auth | 03-Djoser library | /auth/users/activation/ | POST | uid - token | - | Activate user account |
19 | 01-Auth | 03-Djoser library | /auth/users/resend_activation/ | POST | - | Resend activation link to email | |
20 | 01-Auth | 03-Djoser library | /api/groups/manager/users/ | POST, DELETE | - | OAuth 2.0 (Token + Header Prefix=Token) | Add user to group |
21 | 01-Auth | 04-JWT(JSON Web Token) | /api/token/ | POST | email - password | - | Get access and refresh tokens |
22 | 01-Auth | 04-JWT(JSON Web Token) | /api/secret | GET | - | Bearer Token(Token=token) | Test access token |
23 | 01-Auth | 04-JWT(JSON Web Token) | /api/token/refresh/ | POST | refresh | - | Renew access token |
24 | 01-Auth | 04-JWT(JSON Web Token) | /api/token/blacklist/ | POST | refresh | - | Add refresh token to blacklist |
25 | 02-Shop | 01-Simple API | /api/products/ | GET | - | - | Get all products |
26 | 02-Shop | 02-Simple DRF | /api/products/ --- /api/products/{pk} | GET, POST, PUT, PATCH, DELETE | - | - | Get all products using different routes |
27 | 02-Shop | 03-ViewSet | /api/products/ --- /api/products/{pk} | GET, POST, PUT, PATCH, DELETE | - | - | Use ViewSet in view |
28 | 02-Shop | 04-(ModelViewSet) (ReadOnlyModelViewSet) | /api/products/ --- /api/products/{pk} | GET, POST, PUT, PATCH, DELETE | - | - | Use ModelViewSet in view |
29 | 02-Shop | 04-(ModelViewSet) (ReadOnlyModelViewSet) | /api/products/ --- /api/products/{pk} | GET(list-retrieve) | - | - | Use ReadOnlyModelViewSet in view |
30 | 02-Shop | 05-Generic views | /api/products/ | GET, POST | - | - | - |
31 | 02-Shop | 05-Generic views | /api/products// | GET, PUT, PATCH, DELETE | - | - | - |
32 | 02-Shop | 05-Generic views | /api/products/create/ | POST | - | - | - |
33 | 02-Shop | 05-Generic views | /api/products//retrieve/ | GET | - | - | - |
34 | 02-Shop | 05-Generic views | /api/products//destroy/ | DELETE | - | - | - |
35 | 02-Shop | 05-Generic views | /api/products//update/ | PUT, PATCH | - | - | - |
36 | 02-Shop | 05-Generic views | /api/products//retrieve-update/ | GET, PUT, PATCH | - | - | - |
37 | 02-Shop | 05-Generic views | /api/products//retrieve-destroy/ | GET, DELETE | - | - | with optional authentication |
38 | 02-Shop | 06-Serialization | /api/products/ | GET | - | - | Retrieve products with serializer |
39 | 02-Shop | 06-Serialization | /api/products/ | GET | - | - | Retrieve a product with serializer |
40 | 02-Shop | 06-Serialization | /api/products/ | GET | - | - | Retrieve products with related serializer |
41 | 02-Shop | 06-Serialization | /api/products/ | GET | - | - | Retrieve products with related serializer |
42 | 02-Shop | 06-Serialization | /api/products/ | GET | - | - | Retrieve products with serializer linked to category |
43 | 02-Shop | 07-Deserializer | /api/products/ | POST | - | - | Deserialize with post |
44 | 02-Shop | 08-Filtering&Searching&ordering | /api/products/?category=shows&to_price=25 | GET | - | - | Filter by category and lower price limit |
45 | 02-Shop | 08-Filtering&Searching&ordering | /api/products/?search=t-shirt | GET | - | - | Search by product name |
46 | 02-Shop | 08-Filtering & Searching & ordering | /api/products/?ordering=price,inventory,… | GET | - | - | Ordering products |
47 | 02-Shop | 09-Validation | /api/products/ | POST | - | - | First method for validation |
48 | 02-Shop | 09-Validation | /api/products/ | POST | - | - | Second method for validation |
49 | 02-Shop | 09-Validation | /api/products/ | POST | - | - | Third method for validation |
50 | 02-Shop | 09-Validation | /api/products/ | POST | - | - | Fourth method for validation |
51 | 02-Shop | 09-Validation | /api/products/ | POST | - | - | Validation for uniqueness |
52 | 02-Shop | 09-Validation | /api/products/ | POST | - | - | Validation for uniqueness of multiple fields |
53 | 02-Shop | 10-Data sanitization | /api/products/ | POST | - | - | Script injection |
54 | 02-Shop | 11-Pagination | /api/products?perpage=2&page=2 | - | - | - | Pagination as a function-based approach |
55 | 02-Shop | 12-ClassBase Pagination & Filtering | /api/products/?ordering=price,inventory,… | GET | - | - | Filtering for multiple fields |
56 | 02-Shop | 12-ClassBase Pagination & Filtering | /api/products/?ordering=price&page=1 | GET | - | - | Combination of filter and pagination |
57 | 02-Shop | 12-ClassBase Pagination & Filtering | /api/products/?page=2&search=shirt | GET | - | - | Combination of search and pagination |
-
Fork the repository.
-
Create a new branch (
git checkout -b feature-xyz
). -
Commit your changes (
git commit -am 'Add feature xyz'
). -
Push to the branch (
git push origin feature-xyz
). -
Create a new Pull Request.
This project is licensed under the MIT License.
MIT License
Copyright (c) [2024] [Mehdi Emadi]
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit others to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.