Updated May 22, 2023
Introduction to Django Request
Web API Requests are a part of the REST framework structure. The REST frameworks expand as Representational State Transfer. These REST frameworks are responsible for web API data communication. The REQUEST is a component of the REST framework structure. The REQUEST is responsible for passing a request into the web API system; based on the Request posted, the API system makes processing and delivers the response. The web API system calculates and provides the necessary output based on the incoming request. The Django setup formulates the request APIs using an optimized and stable structuring technique.
HTTP Methods
Below, we list the various methods that Django utilizes in the request.method:
HTTP Methods | Description |
GET | The GET method requests a specified resource and retrieves the data. The response for GET will have a HEAD and BODY. |
PUT | The PUT method is used for updating the specified resource. |
POST | The POST method is used for submitting an entity to a specified resource. |
HEAD | The HEAD method is very similar to GET, but the response for a HEAD doesn’t have a body segment. |
DELETE | The DELETE method is used for deleting a resource. |
CONNECT | The CONNECT method establishes a connection between the Target and the source. |
OPTIONS | Mentions all the options between Target and source |
TRACE | It depicts the path between Target and Source |
PATCH | Any partial modifications are applied to the PATH resource. |
Request Framework Attributes
The attributes of the Django request framework are listed below,
Django Request attributes | Description |
HttpRequest.scheme | The request scheme is represented as a string. |
HttpRequest.body | A byte string representation of the request body |
HttpRequest.path | The entire path of the requested page is printed |
HttpRequest.path_info | This mentions the path info portion of the path |
HttpRequest.method | Denotes the type of HTTP request triggered, e.g., GET, POST, etc |
HttpRequest.encoding | Mentions the type of encoding used with the request; if the encoding is not specified, it is mentioned as None. |
HttpRequest.content_type | This Denotes the MIME type of the request, parsed from the CONTENT_TYPE header. |
HttpRequest.content_params | All the key-value parameters mentioned in the Content-type Header will be denoted here. |
HttpRequest.GET | Returns the parameter for GET |
HttpRequest.POST | Returns the parameter for POST |
HttpRequest.COOKIES | All COOKIES details are denoted here |
HttpRequest.FILES | It contains all the uploaded files. |
HttpRequest.META | All HTTP headers are denoted in the META attribute. |
HttpRequest.resolver_match | It contains an instance of ResolverMatch representing the resolved URL. |
Request Framework Methods
All methods associated with the request framework are mentioned below,
Django Request methods | Description |
HttpRequest.get_host() | Returns the current host details. |
HttpRequest.get_port() | Returns the currently connected port details. |
HttpRequest.get_full_path() | Returns the entire path. |
HttpRequest.build_absolute_uri (location) | It returns the absolute URI form of a location. |
HttpRequest.get_signed_cookie (key, default=RAISE_ERROR, salt=”, max_age=None) | Assigns a cookie value for a signed cookie |
HttpRequest.is_secure() | Denotes True if the connection is secure |
HttpRequest.is_ajax() | Denotes True if the request is made via Ajax setup |
Examples of Django Request
Different examples are mentioned below:
Example #1
Design the choiceField() in the forms.py with the values it needs to display and process in the choices attribute of the field.
forms.py:
from django import forms
class requestcheckform(forms.Form):
request_attribute = forms.ChoiceField(choices=[('Request Header','Request Header'),
('Request POST','Request POST'),
('Request Files','Request Files'),
('Request GET','Request GET'),
('Request User','Request User'),
('Request Body','Request Body'),
('Request Content Type','Request Content Type'),
('Request Encoding','Request Encoding'),
('Request Method','Request Method'),
('Request Cookies','Request Cookies'),
('Request Path','Request Path'),
('Request META','Request META'),
('Request port','Request port'),
('Request host','Request host'),
('Request is_secure','Request is_secure'),
Example #2
Formulate the view function for capturing all the request attributes and methods and post them to the messages. Success () prompt allows us to prompt the messages retrieved again into the screen.
views.py:
from django.shortcuts import render
from django.http import HttpResponse
from Django_app1.forms import Valueform,fileform,emailform,requestcheckform
import requests
def requestmethods(request):
form = requestcheckform()
if request.method == 'POST':
# REQUEST OBJECT ATTRIBUTES
if request.POST['request_attribute'] == 'Request Header':
data_content = "data content of request method: " + str(request.headers)
messages.success(request,data_content)
elif request.POST['request_attribute'] == 'Request POST':
post_content = "post content of request method: " + str(request.POST)
messages.success(request,post_content)
elif request.POST['request_attribute'] == 'Request Files':
FILES_content = "FILES in request method: " + str(request.FILES)
messages.success(request,FILES_content)
elif request.POST['request_attribute'] == 'Request GET':
GET_content = "GET Content in request method: " + str(request.GET)
messages.success(request,GET_content)
elif request.POST['request_attribute'] == 'Request User':
Request_User = "User Details: " + str(request.user)
messages.success(request,Request_User)
elif request.POST['request_attribute'] == 'Request Body':
Request_body = "Request Body: " + str(request.body)
messages.success(request,Request_body)
elif request.POST['request_attribute'] == 'Request Content Type':
Request_Content_Type = "Request Content type: " + str(request.content_type)
messages.success(request,Request_Content_Type)
elif request.POST['request_attribute'] == 'Request Encoding':
Request_Encoding = "Request Encoding Used: " + str(request.encoding)
messages.success(request,Request_Encoding )
elif request.POST['request_attribute'] == 'Request Method':
Request_method = "Request Method posted: " + str(request.method)
messages.success(request,Request_method )
elif request.POST['request_attribute'] == 'Request Path':
Request_path = "Path of the request: " + str(request.path)
messages.success(request,Request_path )
elif request.POST['request_attribute'] == 'Request Cookies':
Request_Cookies = "Cookies associated to the Request: " + str(request.COOKIES)
messages.success(request,Request_Cookies )
elif request.POST['request_attribute'] == 'Request META':
Request_META = "HTTP headers info: " + str(request.META)
messages.success(request,Request_META )
# REQUEST METHODS
elif request.POST['request_attribute'] == 'Request port':
Request_port = "Request port number: " + str(request.get_port())
messages.success(request,Request_port )
elif request.POST['request_attribute'] == 'Request host':
Request_host = "Requested Host: " + str(request.get_host())
messages.success(request,Request_host)
elif request.POST['request_attribute'] == 'Request is_secure':
Request_secure = "Security level of the request: " + str(request.is_secure())
messages.success(request,Request_secure)
return render(request,'Request_methods_check.html',{"form":form})
Example #3
Design the webpage in the corresponding template file for this page,
Request_methods_check.html:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Django App1</title>
{% load static %}
<link href="{% static 'admin/css/font.css' %}" rel="stylesheet">
<style>
body {
background-image: url("{% static 'admin/img/background1.jpeg' %}");
background-color: #acccbb;
}
</style>
</head>
<body>
<h1> <u> HTTP METHODS CHECK </u> </h1>
<form method='POST'>
{% csrf_token %}
<button type='submit' onclick="message()"> Click to check Head() and OPTIONS() response </button>
</form>
<form method='GET'>
{% csrf_token %}
<button type='submit' onclick="message()"> Click to check GET() response </button>
</form>
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
</body>
</html>
Output:
Conclusion – Django Request
All methods, attributes, and elements of the request API structure are briefly explained with suitable examples for every request item triggered.
Recommended Articles
We hope that this EDUCBA information on “Django Request” was beneficial to you. You can view EDUCBA’s recommended articles for more information.