Django 视图函数页面返回类型

  1. 返回页面用render()

  2. 返回数据用 HttpResponse()

  3. 重定向用 redirect()

  4. 返回json数据用 JsonResponse()

from django.shortcuts import render,redirect
from django.http import HttpResponse
# Create your views here.

# request就是httpRequest类型的对象,其包含了浏览器请求的信息
def index(request):
    '''首页'''
    # 返回页面用render
    return render(request,'booktest/index.html')

def show_arg(request,num):
    # 返回数据用 HttpResponse
    return HttpResponse(num)


def login(request):
    '''显示登录页面'''
    return render(request,'booktest/login.html')

def login_check(request):
    '''显示登录检验视图'''
    # request.POST  保存的是POST提交的参数
    # request.GET  保存的是GET提交的参数
    # 获取提交的用户名和密码
    print('获取提交的用户名和密码:',type(request.POST),request.POST.get('username'),request.POST.get('password'))  # <class 'django.http.request.QueryDict'>
    print('查看请求的方法:',request.method)  # POST/GET
    print('查看请求的页面路径:',request.path)  #  /login_check
    print('查看提交数据的编码方式:',request.encoding)  # None
    # 进行登录的校验
    username = request.POST.get('username')
    password = request.POST.get('password')

    # 返回应答
    if username == 'mak' and password == '123':
        # 用户密码正确
        # 重定向用 redirect
        return redirect('/index')
    else:
        # 用户密码错误,
        return redirect('/login')

def ajax_test(request):
    # 返回ajax页面
    return render(request,'booktest/test_ajax.html')


def ajax_handle(request):
    # 处理ajax的请求,返回json,html里success(function(data)中的data接受
    return JsonResponse({'res':1})

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值