当前位置: 代码网 > it编程>编程语言>Java > Django学习(二)

Django学习(二)

2024年08月02日 Java 我要评论
当我门直接访问时会出触发django的csrf检测。

get请求

练习:

views.py

def test_method(request):
    if request.method == 'get':
        print(request.get)
        # 如果链接中没有参数a会报错
        print(request.get['a'])
        # 使用这个方法,当查询不到参数时,不会报错而是返回你设置的值
        print(request.get.get('c','no c'))
        # 当链接中传入多个a时,会返回列表;如果使用上面的两个方法时,只会返回最后一个值
        print(request.get.getlist('a'))
    elif request.method == 'post':
        pass
    return httpresponse('ok')

urls.py

    path('test_method', views.test_method)

地址:

http://localhost:8000/test_method?a=1

响应:

post请求:

 

 练习:

views.py

form = """
<form action="/test_method" method="post">
用户名: <input type="text" name="name">
<input type="submit" value="提交">
</form>
 """


def test_method(request):
    if request.method == 'get':
        print(request.get)
        # 如果链接中没有参数a会报错
        print(request.get['a'])
        # 使用这个方法,当查询不到参数时,不会报错而是返回你设置的值
        print(request.get.get('c', 'no c'))
        # 当链接中传入多个a时,会返回列表;如果使用上面的两个方法时,只会返回最后一个值
        print(request.get.getlist('a'))
        return httpresponse(form)
    elif request.method == 'post':
        print(request.post['name'])
        return httpresponse('post ok')
    return httpresponse('ok')

urls.py

    path('test_method', views.test_method)

链接: http://localhost:8000/test_method?a=1

当我门直接访问时会出触发django的csrf检测

关闭csrf检测的方法

post处理:

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com