码迷,mamicode.com
首页 > 其他好文 > 详细

Django-视图函数View

时间:2020-06-27 09:36:43      阅读:50      评论:0      收藏:0      [点我收藏+]

标签:响应   code   页面   函数   访问   else   字符串   python   pass   

请求相关的方法(request--HttpRequest对象)

	print(request.GET)  # 包含所有的HTTP  GET 参数的类字典对象
    print(request.META)  # 请求头信息
    return render(request, ‘project_home.html‘)
    print(request.method)  # 请求中使用的HTTP方法的字符创表示,全大写表示
    print(request.body)  # a1=v1&a2=v2GET请求的原始数据
    print(request.path)
    print(request.get_full_path())#/index/?username=dazhuang &password=123
    print(request.path_info)  # /index返回用户访问url,不包括域名

    print(request.POST)  # 包含所有的HTTP  POST参数的类字典对象

响应相关的方法

HttpResponse  -------回复字符串
render-----回复一个html页面
redirect----重定向
重定向实例
    def login(request):
        if request.method==‘GET‘:
            return render(request,‘login.html‘)
        else:
            username=request.POST.get(‘username‘)
            password=request.POST.get(‘password‘)
            if username==‘hanwujie‘ and password==‘123‘:
                # return render(request,‘home.html‘)
                return redirect(‘/home/‘)
            else:
                return HttpResponse(‘你不是我们的会员,请先注册‘)

    def home(request):
        return render(request,‘home.html‘)

补充 :

http://127.0.0.1:8000/?username=dazhuang &password=123

ip地址/?------ 这个?后面是get请求的数据,一般用作查询用?后面的不算做路径的一部分,只是get请求提交的数据一部分

Django-视图函数View

标签:响应   code   页面   函数   访问   else   字符串   python   pass   

原文地址:https://www.cnblogs.com/wannengjie/p/13197067.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!