Django的快捷功能

Django shortcut functions?:

Django的快捷功能

包 django.shortcuts 收集辅助函数和类,跨越MVC的多个层次,换句话说,

那些函数/类 介绍控制耦合用于方便起见

render()
render(request, template_name, context=None, content_type=None, status=None, using=None)[source]

   return render(req,'cmdb/modifyBtn.html',{'info':info})
   
   结合给定的模板使用一个给定的上下文字典,返回一个 HttpResponse object with that rendered text
   
 Django 不提供一个快捷功能函数 返回一个TemplateResponse,因为TemplateResponse的构造提供相同的便利
 
 Required arguments
 
 需要的参数
 
 request
 
 请求对象用于生成这个响应
 
 template_name
 
 模板的全名 来使用或者模板名字的sequence 如果一个sequence给定,
 
 第一个模板被使用
 
 
 额外的参数:
 
 context
 
 字典的值 增加到模板上下文。默认情况下,这是一个空的字典。
 
 如果字典里的值是可调用的,视图会调用它在呈现模板前
 
 例子:
 
 下面面的例子 展现模板 myapp/index.html 使用MIME类型
 
    return render(req,'cmdb/modifyBtn.html',{'info':info})
	
	
def year_archive(request, aaaa):
     print '-------------------------'
     print aaaa
     print '-------------------------'
     #response = "You're looking at first  the results of question %s."
     #return HttpResponse(response % aaaa)
     #return redirect('https://www.sogou.com/')
     return render(request, 'cmdb/year_archive.html', {
        'foo': aaaa,
    })
	
node2:/django/mysite/news/templates/cmdb#vim year_archive.html

<label class="condition">地址</label><input type="text" name="ip" class="equipment_sz" value="{{foo}}">

http://192.168.137.3:9000/articles/9999/

render_to_response()¶

render_to_response(template_name, context=None, content_type=None, status=None, using=None)[source]¶

这个函数在render()引起之前,工作类似   已经被弃用 

redirect()¶

redirect(to, permanent=False, *args, **kwargs)[source]¶

返回HttpResponseRedirect 到一个合适的URL 对于传递的参数

参数是:

1.一个模型:模型的get_absolute_url() function will be called.

2.	

原文地址:https://www.cnblogs.com/hzcya1995/p/13349280.html