根据上一篇文章,有post,那么就有get请求,其余部分不变,就是把post换成get就可以。
#views.py from django.http.response import HttpResponse from django.shortcuts import render,render_to_response import json # Create your views here. def Login1(request): return HttpResponse('这是一个页面') def Index(request): return HttpResponse('Hello world!') def LoginPost(request): if request.method == 'POST': username = request.POST.get("username") return HttpResponse(username) else: return render_to_response('login.html') def LoginGet(request): if request.method == 'GET': result = {} username = request.GET.get("username") mobile = request.GET.get("mobile") date = request.GET.get('date') result['user']=username result['mobile']=mobile result['date']=date #为什么一定要转换成json格式的返回,因为httpresponse返回的只是一个属性,所以一定要用json格式返回 result = json.dumps(result) return HttpResponse(result) else: return render_to_response('login.html')
#urls.py from django.contrib import admin from django.urls import path from appName.views import * urlpatterns = [ path('', Index), path('loginPost/',LoginPost), path('loginGet/',LoginGet), ]
然后去浏览器里面,输入:http://127.0.0.1:8000/loginGet/?username=peixm&mobile=1111&date=20181119
返回以下的结果: