• Django_视图函数(views)



    视图函数:
    作用:视图函数主要负责逻辑的处理,接收web请求并返回web响应,必须有一个形参,必须返回一个HTTPResponse对象

    http请求:HttpRequest对象
    http响应:HttpResponse对象

    HttpRequest属性和方法
    属性:
    request.path:请求页面的全路径,不包括域名

    确定请求方式是GET还是POST
    request.method:请求中使用的http方法的字符串表示。全大写表示。
    例如:if req.method == "GET":
    do_somwthing()
    elif req.method == "POST":
    do_something_else()

    存放GET请求数据
    request.GET:包含所有http GET参数的类字典对象

    存放POST请求数据
    request.POST:包含所有http POST 参数类字典对象
      只能使用if req.method == "POST"判断是否使用了HTTP POST

    request.COOKIES:包含所有cookies的标准python字典对象,keys和values都是字符串

    request.FILES:包含所有上传文件的类字典对象;FILES中的每一个key都是
    <input type="file" name=""/>标签中name属性的值,FILES中的每一个value也是一个标准的python字典对象,包含下面三个keys:
    filename:上传文件名,用字符串表示
    content_type:上传文件的Content Type
    content:上传文件的原始内容


    request.user:是一个django.contrib.auth.models.User对象,代表当前登录的用户。。
    如果访问用户当前没有登录,user将被初始化为django.contrib.auth.models.AnonymousUser的实例。
    你可以通过user的is_authrnticated()来辨别用户是否登录。
    只有激活django中的AuthenticationMiddleware时该属性才可用

    request.session:唯一可读写的属性,代表当前会话的字典对象;激活django中的session支持时才 可用

    方法:
    get_full_path()
    例如:http://127.0.0.1:8080/index/?name=123,
    req.get_full_path()得到的结果就是/index/?name=123


    HTTPResponse对象
    页面渲染:render()和render_to_response()作用一样,唯一的区别是render需要加request
    页面跳转:redirect(“路径”)
    locals():可以直接将函数中所有的变量传给模板

    实例:views文件部分
    from django.shortcuts import render,HttpResponse,redirect

    # Create your views here.

    import time
    def show_time1(request):

    t1 = time.ctime()
    # return HttpResponse("hello")
    #return render(request,"index.html",locals()) ,等于下面的
    return render(request,"index.html",{"time":t1})

    #输出固定的2004
    def article_year0(request):
    return HttpResponse("2004")
    #根据输入来输出
    def article_year(request,year):
    return HttpResponse(year)
    def article_year_month(request,year,month):
    return HttpResponse("year:%s month:%s"%(year,month))
    def article_year_month_day(request,year,month,day):
    return HttpResponse("year:%s month:%s day:%s"%(year,month,day))

    def register(request):
    if request.method == "POST":
    print(request.POST.get("user"))
    print(request.POST.get("age"))
    print(request.POST.get("hobby"))
    user = request.POST.get("user")
    if user == "kang":
    return redirect('/login/') #跳转到页面login

    return HttpResponse("success!")
    return render(request,"register.html")

    def views1(request):
    print(request.path) #返回view1的路径
    print(request.get_full_path()) #返回view1的路径,可以带数据,与上面的等价
    print("method=",request.method) #确定请求方式是GET还是POST
    print("request.GET=",request.GET) #<QueryDict: {}>
    print("request.POST=",request.POST) #<QueryDict: {}>
    print("request.COOKIES=",request.COOKIES) #{}
    print("request.FILES=",request.FILES)
    print("request.user=",request.user) #当前登录的用户


    return HttpResponse("这是视图练习")

    def login(req):

    return render(req,'login.html')
     
  • 相关阅读:
    GlusterFS分布式文件系统部署
    bower解决js的依赖管理
    Linux如何查看当前占用CPU或内存最多的几个进程
    WIN7无法卸载掉中文繁体注音输入法
    kafka消费者报错INVALID_FETCH_SESSION_EPOCH
    Hbase报错:org.apache.hadoop.hbase.ipc.ServerNotRunningYetException: Server is not running yet
    Prometheus一条告警是怎么触发的
    prometheus client_golang使用
    prometheus告警插件-alertmanager
    sed替换变量
  • 原文地址:https://www.cnblogs.com/shadowfolk/p/15012632.html
Copyright © 2020-2023  润新知