The package django.shortcuts collects helper functions and classes that “span” multiple levels of MVC. In other words, these functions/classes introduce controlled coupling for convenience’s sake.
django.shortcuts包含贯穿多级MVC的辅助函数与类,换言之,这些函数和类为了方便的缘故引进了受约束的耦合
render()
render(request, template_name, context=None, content_type=None, status=None, using=None)[source]
Combines a given template with a given context dictionary and returns an HttpResponse object with that rendered text.(将给定的模板与上下文字典结合后返回一个带有渲染文本的HttpResponse对象 )
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')
以上内容等同于
This example is equivalent to:
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()
redirect(to, permanent=False, *args, **kwargs)[source]
Returns an HttpResponseRedirect to the appropriate URL for the arguments passed.(对应传递给适当的URL的参数,返回一个HttpResponseRedirect )
The arguments could be(参数可以是以下条目):
- A model(模型): the model’s get_absolute_url() function will be called.
- A view name(视图名称), possibly with arguments: reverse() will be used to reverse-resolve the name.
- An absolute or relative URL(一个绝对或者相对的URL), which will be used as-is for the redirect location.
By default issues a temporary redirect; pass permanent=True to issue a permanent redirect.(默认发送一个临时的重定向,传递permanent=True参数可以发送一个永久的重定向)
By passing some object; that object’s get_absolute_url() method will be called to figure out the redirect URL:传递对象,对象的get_absolute_url()方法将会被调用然后指明要重定向的URL
from django.shortcuts import redirect
def my_view(request):
...
object = MyModel.objects.get(...)
return redirect(object)
By passing the name of a view and optionally some positional or keyword arguments; the URL will be reverse resolved using the reverse() method:通过传递视图名称以及一些位置固定或者重要的参数,URL将会被reverse()函数解析转换
def my_view(request):
...
return redirect('some-view-name', foo='bar')
By passing a hardcoded URL to redirect to:通过传递硬编码的URL进行重定向
def my_view(request):
...
return redirect('/some/url/')
By default, redirect() returns a temporary redirect. All of the above forms accept a permanent argument; if set to True a permanent redirect will be returned:加上permanent参数之后就可以实现永久重定向了
def my_view(request):
...
object = MyModel.objects.get(...)
return redirect(object, permanent=True)
get_object_or_404()
get_object_or_404(klass, *args, **kwargs)[source]
Calls get() on a given model manager, but it raises Http404 instead of the model’s DoesNotExist exception.在一个给定的模型管理器上调用get()函数,但是如果模型的DoesNotExist异常触发的时候将自举一个Http404错误
from django.shortcuts import get_object_or_404
def my_view(request):
my_object = get_object_or_404(MyModel, pk=1)
等同于下:
This example is equivalent to:
from django.http import Http404
def my_view(request):
try:
my_object = MyModel.objects.get(pk=1)
except MyModel.DoesNotExist:
raise Http404("No MyModel matches the given query.")
get_list_or_404()
get_list_or_404(klass, *args, **kwargs)[source]
Returns the result of filter() on a given model manager cast to a list, raising Http404 if the resulting list is empty.对于给定的模型返回一个转换为列表的过滤结果,如果列表为空则自举Http404错误
from django.shortcuts import get_list_or_404
def my_view(request):
my_objects = get_list_or_404(MyModel, published=True)
This example is equivalent to:上例同下
from django.http import Http404
def my_view(request):
my_objects = list(MyModel.objects.filter(published=True))
if not my_objects:
raise Http404("No MyModel matches the given query.")