Updated May 20, 2023
Introduction to Django httpresponse
The outcome from a process will be assigned as a response. This response can be of any item which is been formulated and expected to be put as an output to the webpage. Once this output is formulated then it can be encapsulated to a httpresponse method for generating the response object. Once the response object is generated it is assigned to a variable and sent as output. The response methods and attributes are largely useful. They are useful in setting up the attributes and values for the attributes which are used.
Syntax:
response = HttpResponse(response item)
According to the above syntax, you encapsulate the response item, which is intended to be the response, within a HttpResponse method. After that, you assign the response item to a variable. You will use this variable to set up the responses.
Httpresponse Attributes
Httpresponse attribute | Reason |
HttpResponse.content | This attribute denotes the content of the message. |
HttpResponse.charset | A string value representing the character encoding of the response. |
HttpResponse.status_code | This represents the response status code |
HttpResponse.reason_phrase | This represents the reason phrase of the response |
HttpResponse.streaming | Mentions whether it is a streamed communication or not. |
HttpResponse.closed | When the formulated response is closed then this value is assigned as true |
Changes in Models.py file:
from django.db import models
from django.contrib.auth.models import User
# Model variables
# Create your models here.
class Bride(models.Model):
Django_response_Example_name = models.CharField(max_length=200,null=True)
Django_response_Example_thegai = models.CharField(max_length=200,null=True)
Django_response_Example_State = models.CharField(max_length=50,null=True)
Django_response_Example_District = models.CharField(max_length=50,null=True)
Django_response_Example_Address = models.TextField(null=True)
Django_response_Example_Phone = models.BigInteger_Example_Field(null=True)
Django_response_Example_profession = models.CharField(max_length=200,null=True)
Django_response_Example_salary = models.BigInteger_Example_Field(null=True)
Django_response_Example_Under_Graduation_Degree = models.CharField(max_length=200,null=True)
Django_response_Example_Under_Graduation_college = models.CharField(max_length=400,null=True)
Django_response_Example_Post_Graduation_Degree = models.CharField(max_length=200,null=True)
Django_response_Example_Post_Graduation_college = models.CharField(max_length=400,null=True)
Django_response_Example_Rasi = models.CharField(max_length=200,null=True)
Django_response_Example_Nakshatra = models.CharField(max_length=200,null=True)
def __str__(self):
return self.name
Create a view for the form:
The render import proves to be highly beneficial for rendering the HTML document in the browser. Following that, the HTTP response is imported. Within the views method, you instantiate the Value shape and assign it to a variable called “shape”. At the beginning of the instantiation, you display the validity of the shape value. If the value of the shape is valid, you will complete the save process of the shape. The shop technique will take location with the shape.shop() method. Next, you can fetch and save the information of the currently logged-in user onto the shape. This is how the shape garage will take location. Once you have successfully saved the shape, you will render it directly to the browser using a render method.
def Main_page(request):
Post_keys = []
bride_id_str_list = []
brides = Bride.objects.all()
# Bride_Image = brides.Image
# context2['Image'] = Bride_Image
context = {'brides':brides}
if request.method == 'GET':
State = request.GET.get('state', '')
District = request.GET.get('district', '')
thegai = request.GET.get('thegai', '')
Rasi = request.GET.get('Rasi', '')
print(len(State),len(District),District, Rasi)
if len(State) > 0:
Filter_context = {}
bride = Bride.objects.filter(State=str(State))
Filter_context = {'brides':bride}
return render(request,'Mainpage.html',Filter_context)
if len(District) > 0:
Filter_context = {}
bride = Bride.objects.filter(District=str(District))
print(bride)
Filter_context = {'brides':bride}
return render(request,'Mainpage.html',Filter_context)
if len(thegai) > 0:
Filter_context = {}
bride = Bride.objects.filter(thegai=str(thegai))
print(bride)
Filter_context = {'brides':bride}
return render(request,'Mainpage.html',Filter_context)
if len(Rasi) > 0:
Filter_context = {}
bride = Bride.objects.filter(Rasi=str(Rasi))
print(bride)
Filter_context = {'brides':bride}
return render(request,'Mainpage.html',Filter_context)
print("Content of the resposne: ",response.content)
print("Charecterset of the response: ",response.charset)
print("Status code of the response: ",response.status_code)
print("Reason phrase of the response: ",response.reason_phrase)
print("Reason close status: ",response.closed)
return render(request,'Mainpage.html',context)
@login_required
def profile_reg_user(request):
Filter_context = {}
current_user = request.user
bride = Bride.objects.filter(Creator=current_user)
Filter_context = {'brides':bride}
print("Content of the resposne: ",response.content)
print("Charecterset of the response: ",response.charset)
print("Status code of the response: ",response.status_code)
print("Reason phrase of the response: ",response.reason_phrase)
print("Reason close status: ",response.closed)
return render(request,'Profiles_reg_user.html',Filter_context)
@login_required
def form_update(request,pk):
update_profile = Bride.objects.get(id=pk)
form = Valueform(instance=update_profile)
context = {'form':form}
if request.method == 'POST':
form = Valueform(request.POST,instance=update_profile,files=request.FILES)
print(form)
if form.is_valid():
post = form.save()
post.Creator = request.user
print('Creator user stored',request.user)
post.save()
obj = form.instance
return render(request,'form.html', {"obj": obj,"form": form})
print("Content of the resposne: ",response.content)
print("Charecterset of the response: ",response.charset)
print("Status code of the response: ",response.status_code)
print("Reason phrase of the response: ",response.reason_phrase)
print("Reason close status: ",response.closed)
return render(request,'form.html', {"form": form})
url.py:
from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from matrimony_pages import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'Jsoncheck/',views.Json_Response,name='Json_Response'),
url(r'^$',views.Welcome_page,name='Welcome_page'),
url(r'Mainpage/',views.Main_page,name='Main_page'),
url(r'all/',views.All_users,name='all'),
url(r'form/',views.form_view,name='form_view'),
url(r"signup/", views.Sign_up_request, name="register"),
url(r"login/", views.login_request, name="login"),
path(r'profile//',views.profile_page,name='profile'),
url(r'logout/',views.logout_request,name='logout'),
url(r'reg/',views.profile_reg_user,name='reg'),
path(r'update//',views.form_update,name='update'),
path('admin/', admin.site.urls),
]+ static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
Form.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/background.jpg' %}");
background-color: #acccbb;
}
.myDiv {
border: 5px outset red;
background-color: lightblue;
text-align: center;
font-family: "Comic Sans MS", cursive, sans-serif;
font-size: 14px;
letter-spacing: 2px;
word-spacing: 1.8px;
text-align: left;
color: #02071C;
font-weight: 200;
text-decoration: none;
font-style: normal;
font-variant: normal;
text-transform: capitalize;
}
</style>
</head>
<body>
<h1> <u> DJANGO HANDELING EMAILS </u> </h1>
<nav class='navbar'>
<div class='navbar_div'>
<a class="navbar" onclick="redirect2()" >Home! </a>
<a class="navbar" onclick="redirect2()" >Contact</a>
</div>
</nav>
<div class="tablediv">
<script>
function form1() {
window.location.href = "http://127.0.0.1:8000/form";
}
function redirect1() {
window.location.href = "http://127.0.0.1:8000/Mainpage";
}
function redirect2() {
window.location.href = "http://127.0.0.1:8000/";
}
function redirect3() {
window.location.href = "http://127.0.0.1:8000/login";
}
function redirect4() {
window.location.href = "http://127.0.0.1:8000/signup";
}
function redirect5() {
window.location.href = "http://127.0.0.1:8000/";
}
function redirect6() {
window.location.href = "http://127.0.0.1:8000/logout";
}
/* Open when someone clicks on the span element */
function openNav() {
document.getElementById("myNav").style.width = "100%";
}
/* Close when someone clicks on the "x" symbol inside the overlay */
function closeNav() {
document.getElementById("myNav").style.width = "0%";
}
</script>
<div class="body">
<br>
{% block content %}
<form method="PUT" class='formarea' enctype="multipart/form-data">
<div class='formdiv'>
{{ form.as_p }}
{% csrf_token %}
<input type="submit" class='button' value="Update">
</div>
</form>
{% endblock content %}
<div class="myDiv" style = "max-width:470px;">
<form method = 'POST' ">
{{ email.as_p }}
{% csrf_token %}
<input type="submit" class="btn btn-primary" value="submit" style="text-align:center">
</form>
</div>
</body>
</html>
Output:
Conclusion – Django httpresponse
The article clearly explains how the Django framework creates the HTTP response item. Additionally, it describes the process of printing the response value back in the console. The console displays the values.
Recommended Articles
We hope that this EDUCBA information on “Django httpresponse” was beneficial to you. You can view EDUCBA’s recommended articles for more information.