视频加载失败

Django便捷函数shortcuts

864 字
4 分钟
Django便捷函数shortcuts

【一】Django便捷函数#

【1】介绍#

  • 包 django.shortcuts 收集助手函数和“跨”多级mvc的类,换句话说,为了方便起见,这些函数/类引入受控耦合。
from django.shortcuts import render, HttpResponse,redirect,reverse,resolve_url

【2】官方链接#

【二】render()#

【1】语法#

def render(request, template_name, context=None, content_type=None, status=None, using=None):
"""
Return a HttpResponse whose content is filled with the result of calling
django.template.loader.render_to_string() with the passed arguments.
"""
content = loader.render_to_string(template_name, context, request, using=using)
return HttpResponse(content, content_type, status)
  • 将给定的模板与给定的上下文字典组合在一起,并以渲染的文本返回一个 HttpResponse 对象。
  • Django没有提供返回 TemplateResponse 的便捷函数,因为 TemplateResponse 的构造函数提供了与 render() 同等程度的便利。

【2】可选参数#

(1)request#

  • 用于生成此响应的请求对象。

(2)template_name#

  • 要使用的模板的全名或模板名称的序列。
    • 如果给定一个序列,则将使用存在的第一个模板。
    • 有关如何查找模板的更多信息,请参见 模板加载文档 。

【3】可选参数#

(1)context#

  • 要添加到模板上下文的值的字典。
  • 默认情况下,这是一个空的字典。
  • 如果字典中的值是可调用的,则视图将在渲染模板之前调用它。

(2)content_type#

  • 用于结果文档的 MIME 类型。默认 ‘text/html’ 。

(3)status#

  • 响应的状态码默认为 200。

(4)using#

  • 用于加载模板的模板引擎的 NAME 。

【示例】#

from django.shortcuts import render
def my_view(request):
# View code here...
return render(request, 'myapp/index.html', {
'foo': 'bar',
}, content_type='application/xhtml+xml')
  • 等同于
from django.http import HttpResponse
from django.template import loader
def my_view(request):
# View code here...
t = loader.get_template('myapp/index.html')
c = {'foo': 'bar'}
return HttpResponse(t.render(c, request), content_type='application/xhtml+xml')

【三】redirect()#

【1】语法#

def redirect(to, *args, permanent=False, **kwargs):
"""
Return an HttpResponseRedirect to the appropriate URL for the arguments
passed.
The arguments could be:
* A model: the model's `get_absolute_url()` function will be called.
* A view name, possibly with arguments: `urls.reverse()` will be used
to reverse-resolve the name.
* A URL, which will be used as-is for the redirect location.
Issues a temporary redirect by default; pass permanent=True to issue a
permanent redirect.
"""
redirect_class = HttpResponsePermanentRedirect if permanent else HttpResponseRedirect
return redirect_class(resolve_url(to, *args, **kwargs))

【2】参数#

  • 一个模型:模型的 get_absolute_url() 函数将被调用。
  • 视图名,可能带有的参数:reverse() 将被用于反向解析名称。
  • 一个绝对或相对 URL,将按原样用作重定向位置。
  • 默认情况下发出临时重定向;通过传递 permanent=True 发出永久重定向。

【示例】#

(1)传递对象#

from django.shortcuts import redirect
def my_view(request):
...
obj = MyModel.objects.get(...)
return redirect(obj)

(2)传递视图名和一些可选的位置或关键字参数#

  • URL 将使用 reverse() 方法来反向解析:
def my_view(request):
...
return redirect('some-view-name', foo='bar')

(3)传递硬编码 URL 来重定向#

def my_view(request):
...
return redirect('/some/url/')
  • 这也适用于完整的 URL :
def my_view(request):
...
return redirect('https://example.com/')

【补充】默认返回临时重定向#

  • 默认情况下,redirect() 返回临时重定向。
  • 所有以上形式都接受 permanent 参数;如果设置为 True 会返回一个永久重定向:
def my_view(request):
...
obj = MyModel.objects.get(...)
return redirect(obj, permanent=True)

【扩展阅读】 临时重定向和永久重定向的区别#

  • 临时重定向(响应状态码:302)和永久重定向(响应状态码:301)对普通用户来说是没什么区别的,它主要面向的是搜索引擎的机器人。
    • A页面临时重定向到B页面,那搜索引擎收录的就是A页面。
    • A页面永久重定向到B页面,那搜索引擎收录的就是B页面。

支持与分享

如果这篇文章对你有帮助,欢迎分享给更多人或打赏支持!

打赏
Profile Image of the Author
Dream
Hello, I'm Dream.
公告
欢迎来到我的博客!爱学习爱进步。
分类
标签
最新动态
站点统计
文章
300
分类
5
标签
26
总字数
640,882
运行时长
0 天
最后活动
0 天前
站点信息
构建平台
Vercel
博客版本
Firefly v6.16.8
文章许可
CC BY-NC-SA 4.0
文章目录