zoukankan      html  css  js  c++  java
  • (8)视图层参数request详解

    PS:浏览器页面请求的都是get请求

    PS:post请求是通过form表单,ajax请求

    request里面的常用方法

    def index(request):
      print(request.META)   #请求对象的所有内容都包含在了这个META里面,包括访问的地址等等信息
        #request就是一个对象,请求对象,请求的所有东西都被封装到requres里
    print(request.method) #请求方式分get和post,如果是get请求,则method打印出来的是get,同理post
    print(request.path) #请求的地址
    print(request.get_full_path()) #请求的全路径
    print(request.GET) #请求get形式传的参数,全都在这里,以字典形式
    print(request.body) #请求体的内容
    print(request.POST) #以post请求的参数全在这里
    return render(request,'index.html')

     

    PS:当requests.POST的时候发送数据使用data封装的,取值的时候直接用post取值即可,如果发送数据时候是用json封装的,则取值的时候必须在body中取值 

    request参数实例

    urls.py  #这个是总路由

    from django.conf.urls import url,include  #include就是用来做路由分发的
    from django.contrib import admin


    from app01 import views
    urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'index/',views.index)
    ]

     index.html   #模板层内

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>我是首页</title>
    </head>
    <body>
    <h1>django的index页面</h1>
    {# action:请求的地址 / post:请求的方式 #}
    {# action里的地址可以写全路径,也可以只写一个地址名字index,不写也可以就默认当前路径 #}
    <form action="http://127.0.0.1:8000/index/" method="post">
    <p>
    名字:<input type="text" name="name">
    </p>
    <p>
    密码:<input type="password" name="pwd">
    </p>
    <input type="submit" value="提交">
    </form>
    </body>
    </html>

    views.py   #这个是app的视图层

    from django.shortcuts import render,HttpResponse,redirect,reverse

    def test(request):
    return HttpResponse('我是app01的test')

    def index(request):
    print(request.method)
    print(request.path)
    print(request.get_full_path())
    print(request.GET)
    print(request.body)
    print(request.POST)
    return render(request,'index.html')

    点击提交出现

     注释掉settings里面的

     

    简单的登陆功能实例

    views.py   #app下的视图层

    from django.shortcuts import render,HttpResponse,redirect,reverse

    def test(request):
    return HttpResponse('我是app01的test')

    def login(request):
    if request.method == 'GET':
    return render(request,'login.html')
    elif request.method == 'POST':
    print(request.POST)
    name = request.POST.get('name') #name = request.POST[name] 也可以这样取值,但是当无值的时候会报错
    pwd = request.POST.get('pwd')
    print(name)
    print(pwd)
    if name == 'lqz' and pwd == '123':
    return redirect('http://www.baidu.com')
    else:
    return HttpResponse('用户名或密码错误')

    login.html   #模板层的页面文件

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>登陆页面</title>
    </head>
    <body>
    <form action="http://127.0.0.1:8000/login/" method="post">
    <p>
    名字:<input type="text" name="name">
    </p>
    <p>
    密码:<input type="password" name="pwd">
    </p>
    <input type="submit" value="提交">
    </form>
    </body>
    </html>

    urls.py

    from django.conf.urls import url,include  #include就是用来做路由分发的
    from django.contrib import admin


    from app01 import views
    urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'login/',views.login)
    ]
  • 相关阅读:
    IMWebConf 2017 官网彩蛋解谜
    解决SVG animation 在IE中不起作用
    百度大搜和度秘面经
    浅谈JavaScript原型与原型链
    听说2017你想写前端?
    如何制作icon-font小图标
    HTML5 CSS3 诱人的实例 :模仿优酷视频截图功能
    javaweb action无法跳转、表单无法跳转的解决方法
    hadoop备战:yarn框架的搭建(mapreduce2)
    liferay 指定默认首页
  • 原文地址:https://www.cnblogs.com/shizhengquan/p/10489111.html
Copyright © 2011-2022 走看看