Django Response(Django response framework属性)11

作者 : 慕源网 本文共6732个字,预计阅读时间需要17分钟 发布时间: 2021-11-20 共138人阅读

本文是Django教程的一部分,您可以点击Django教程系列查看所有教程。

Django Response(Django response framework属性)11

Django Response 简介

响应是网页处理的结果,当使用 Http 响应类缓存返回值时,可以使用 Http 响应类捕获该结果,然后该响应将被分配面向响应的特性,就像一个请求框架可以使用特殊的属性和方法来捕获和验证这些特征,这些属性和方法中的每一个都显示与响应关联的相应值,更具体地说,这些响应方法和属性在验证与响应相关的各种信息时很有用。所有这些属性和方法将在下面通过合适的示例进行简要讨论。在本主题中,我们将了解 Django Response。

Response Framework属性

下面列出了Django response framework的属性,

HttpResponse 属性 原因
HttpResponse.content 该属性用于表示消息的内容
HttpResponse.charset 表示响应字符编码的字符串值。
HttpResponse.status_code 这代表响应状态代码
HttpResponse.reason_phrase 这代表响应的原因短语
HttpResponse.streaming 提及它是否是流式通信。
HttpResponse.closed 当制定的响应关闭时,则将此值指定为 true

Response Framework方法

下面提到了与response framework相关的所有方法,

HttpResponse 属性 描述
HttpResponse.__init__(content=”, content_type=None, status=200, reason=None, charset=None) 内容页面和内容类型与响应对象相关联
HttpResponse.__setitem__(header, value) 该值与标头名称相关联
HttpResponse.__delitem__(header) 删除特定的标题
HttpResponse.__getitem__(header) 返回特定标头名称的值
HttpResponse.has_header(header) 它根据对具有提供名称的标头进行不区分大小写的检查,返回 True 或 False。
HttpResponse.setdefault(header, value) 允许制定默认标题值
HttpResponse.write(content) 使用此创建类文件对象的响应。
HttpResponse.flush() 允许响应对象被刷新
HttpResponse.tell() 将在响应中创建一个类似文件的对象
HttpResponse.getvalue() 用于获取HttpResponse.content 的值。
HttpResponse.readable() 将在响应中创建一个类似流的对象
HttpResponse.seekable() 使响应对象可达

Django Response示例

以下是下面提到的以下示例:

示例#1

1)在forms.py中设计choiceField(),其需要在该字段的choices属性中显示和处理的值。

views.py:

def email_sending(response):
email = emailform()
if response.method == 'POST':
email_id =  response.POST['email'] email_subject =  response.POST['email_subject'] email_message =  response.POST['email_message'] mail = send_mail(email_subject,email_message,'testmysmtpconnection@gmail.com',[email_id],fail_silently = False)
response = HttpResponse(mail)
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 response
return render(response, 'emailpage.html',{"email":email})

2) 在该页面对应的模板文件中设计网页,

Response_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/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>
<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>

输出:

Django Response(Django response framework属性)11

示例#2

1) 这里验证了来自文件上传页面的响应值

views.py:

from django.shortcuts import render
from django.http import  HttpResponse
from Django_app1.forms import Valueform,fileform,emailform,responsecheckform
from django.core.exceptions import ViewDoesNotExist
from django.contrib.auth.models import User
from django.core.files.storage import FileSystemStorage
from django.contrib import messages
from django.core.mail import send_mail,EmailMessage
import responses
def file_upload(response):
file = fileform()
print(" File Values in File Dictionary:", response.FILES)
if response.method == 'POST' and response.FILES['Uploaded_File']:
uploaded_file = response.FILES['Uploaded_File'] fs = FileSystemStorage()
filename = fs.save(uploaded_file.name, uploaded_file)
uploaded_File_Size = 'Size of Uploaded file: ' + str(uploaded_file.size)
content_type_of_uploaded_file = 'Content type of uploaded file: ' + str(uploaded_file.content_type)
uploaded_file_name = 'Name of Uploaded file: ' + str(uploaded_file.name)
uploaded_file_url = fs.url(filename)
print("uploaded file url",uploaded_file_url)
messages.success(response, '!!! File upload successful !!!')
messages.success(response,uploaded_File_Size)
messages.success(response,uploaded_file_name)
messages.success(response,content_type_of_uploaded_file)
response = HttpResponse(filename)
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(response, 'filehandeling.html', {"file":file})
return render(response, 'filehandeling.html',{"file":file})

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;
}
</style>
</head>
<body>
<h1> <u> FILE UPLOAD HANDELING IN DJANGO </u> </h1>
{% if messages %}
<ul>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
<div style = "max-width:470px;">
<form method =  'POST' enctype="multipart/form-data">
{{ file.as_p }}
{% csrf_token %}
<input type="submit" class="btn btn-primary" value="submit">
</form>
</div>
</body>
</html>

输出:

Django Response(Django response framework属性)11

Django Response(Django response framework属性)11

示例 #3

1) 这里从表单页面生成的响应值被捕获和验证,

views.py:

from django.shortcuts import render
from django.http import  HttpResponse
from Django_app1.forms import Valueform,fileform,emailform,responsecheckform
from django.core.exceptions import ViewDoesNotExist
from django.contrib.auth.models import User
from django.core.files.storage import FileSystemStorage
from django.contrib import messages
from django.core.mail import send_mail,EmailMessage
import responses
def formView(response_iter):
form = Valueform()
if response_iter.method == "POST":
value = Valueform(response_iter.POST)
if value.is_valid():
first_name = value.cleaned_data['first_name'] response = HttpResponse(first_name)
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)
if response_iter.session.has_key(first_name):
print(response_iter.session.items())
return render(response_iter, 'Session.html' )
else:
response_iter.session[first_name] = first_name
return render(response_iter, 'Form_Handeling.html', {"form":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/base.css' %}" rel="stylesheet">
<style>
body {
background-image: url("{% static 'admin/img/background1.jpg' %}");
background-color: #acccbb;
}
</style>
</head>
<body>
<h1> <u> FORMS HANDELING IN DJANGO </u> </h1>
<div style = "max-width:470px;">
<form method =  'GET'>
{{ emailform.as_p }}
{% csrf_token %}
<input type="submit" class="btn btn-primary" value="submit">
</form>
</div>
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
</body>
</html>

输出:

Django Response(Django response framework属性)11

结论

通过以上提供的描述和示例,可以了解与Httpresponse framework 相关的关键信息。


慕源网 » Django Response(Django response framework属性)11

常见问题FAQ

程序仅供学习研究,请勿用于非法用途,不得违反国家法律,否则后果自负,一切法律责任与本站无关。
请仔细阅读以上条款再购买,拍下即代表同意条款并遵守约定,谢谢大家支持理解!

发表评论

开通VIP 享更多特权,建议使用QQ登录