|
1 | | -# Django-Request-Response-Model-Method-Header-Cookies-Json-FileUploading-Mastering |
2 | | -Django-Request-Response-Model-Method-Header-Cookies-Json-FileUploading-Mastering |
| 1 | +# Django Request-Response Mastering |
| 2 | + |
| 3 | +This Django project demonstrates **mastering request and response handling**, including plain text, numbers, booleans, JSON, redirects, headers, cookies, query strings, form data, and JSON body handling. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## 🚀 Features |
| 8 | + |
| 9 | +### 1. Response Examples |
| 10 | + |
| 11 | +| Type | Example | |
| 12 | +|------|---------| |
| 13 | +| Simple Plain Text | `HttpResponse('Plane Text Response')` | |
| 14 | +| Number Response | `HttpResponse(str(10))` | |
| 15 | +| Boolean Response | `HttpResponse(str(False))` | |
| 16 | +| JSON Response | `JsonResponse({'Name': 'Raihan', 'age': '18'})` | |
| 17 | +| Redirect Response | `HttpResponseRedirect('/')` | |
| 18 | +| Not Found Response | `HttpResponseNotFound('Page Not Found')` | |
| 19 | +| Custom Status Code | `HttpResponse('Status Code', status=202)` | |
| 20 | +| Custom Header | `resp = HttpResponse('Plane Text'); resp['token']='784001'` | |
| 21 | +| Response Cookies | `resp = HttpResponse('Plane Text Response with Cookie'); resp.set_cookie('Text_Cookie','784001adf')` | |
| 22 | + |
| 23 | +> **Note:** Only one `return` statement is executed per view. Multiple `return` statements cannot all run in one view. |
| 24 | +
|
| 25 | +--- |
| 26 | + |
| 27 | +### 2. Request Examples |
| 28 | + |
| 29 | +| Type | Example | |
| 30 | +|------|---------| |
| 31 | +| Request Methods | `GET`, `POST`, `PUT`, `PATCH`, `DELETE` handled in `TypesOfRequest` | |
| 32 | +| Query String | Access via `request.GET.get('param_name')` e.g., `QueryString` view | |
| 33 | +| Custom Headers | Access via `request.headers.get('header_name')` e.g., `CustomHeader` view | |
| 34 | +| Form Data | Access via `request.POST.dict()` e.g., `FormData` view | |
| 35 | +| JSON Body | Access via `json.loads(request.body)` e.g., `RequestBody` view | |
| 36 | + |
| 37 | +--- |
| 38 | + |
| 39 | +## 📝 Example Usage |
| 40 | + |
| 41 | +### GET Request with Query String |
| 42 | +```http |
| 43 | +GET /QueryString/?name=Raihan&city=Dhaka |
| 44 | +Response: Query String: Raihan, Dhaka |
| 45 | +``` |
| 46 | + |
| 47 | +### POST Request with Form Data |
| 48 | +```http |
| 49 | +POST /FormData/ |
| 50 | +Content-Type: application/x-www-form-urlencoded |
| 51 | +name=Raihan&city=Dhaka |
| 52 | +Response: {"name":"Raihan","city":"Dhaka"} |
| 53 | +``` |
| 54 | + |
| 55 | +### POST Request with JSON Body |
| 56 | +```http |
| 57 | +POST /RequestBody/ |
| 58 | +Content-Type: application/json |
| 59 | +{ |
| 60 | + "name": "Raihan", |
| 61 | + "age": 18, |
| 62 | + "city": "Dhaka" |
| 63 | +} |
| 64 | +Response: |
| 65 | +{ |
| 66 | + "name": "Raihan", |
| 67 | + "age": 18, |
| 68 | + "city": "Dhaka" |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +### Accessing Custom Header |
| 73 | +```http |
| 74 | +GET /CustomHeader/ |
| 75 | +Headers: |
| 76 | +mytoken: 784001 |
| 77 | +password: mypass |
| 78 | +Response: Query String: 784001, mypass |
| 79 | +``` |
| 80 | + |
| 81 | +--- |
| 82 | + |
| 83 | +## ⚡ Setup |
| 84 | + |
| 85 | +```bash |
| 86 | +# Create virtual environment |
| 87 | +python -m venv venv |
| 88 | +source venv/bin/activate # Windows: venv\Scripts\activate |
| 89 | + |
| 90 | +# Install Django |
| 91 | +pip install django |
| 92 | + |
| 93 | +# Start Project & App |
| 94 | +django-admin startproject request_response_mastering |
| 95 | +cd request_response_mastering |
| 96 | +python manage.py startapp myapp |
| 97 | + |
| 98 | +# Add 'core' to INSTALLED_APPS in settings.py |
| 99 | +# Create urls.py in core app and connect views |
| 100 | +# Run migrations and start server |
| 101 | +python manage.py migrate |
| 102 | +python manage.py runserver |
| 103 | +``` |
| 104 | + |
| 105 | +--- |
| 106 | + |
| 107 | +## 📝 Notes |
| 108 | + |
| 109 | +- Use `@csrf_exempt` for POST requests during development/testing. |
| 110 | +- Only one response can be returned from a view. |
| 111 | +- Cookie names cannot have spaces; use letters, digits, underscores, or hyphens. |
| 112 | +- JSON responses are best handled with `JsonResponse`. |
| 113 | +- Query parameters and headers can be accessed via `request.GET` and `request.headers`. |
| 114 | +- Form data can be converted to dict with `request.POST.dict()`. |
| 115 | + |
| 116 | +--- |
| 117 | + |
| 118 | +**Author:** MD. Mostafa Raihan |
| 119 | +**Technology:** Django, Python |
| 120 | +**Purpose:** Learn and master Django Request-Response handling. |
0 commit comments