不传参数,获得内容(例如获得北京电台接口)
def liveRadioBjAPI(request):
BJradio_list = serializers.serialize("json",LiveTelecastRadio.objects.filter(area=2))
return HttpResponse(BJradio_list, content_type='application/json; charset=utf-8')```
输入网址:```http://127.0.0.1:8000/liveRadioBjAPI/```即可
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/1908836-1ad831d0cd3f2156.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
###传参数,两种请求方式(例如输入用户id,获取收藏列表)
#####第一种方式
```http://127.0.0.1:8000/usercollectAPI/?userID=138 输入用户id即可```
def usercollectAPI(request):
if 'userID' in request.GET:
userID=request.GET['userID']
userCollectList=serializers.serialize("json",UserProgramCollect.objects.filter(user_id = userID))
return HttpResponse(userCollectList, content_type='application/json; charset=utf-8')
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/1908836-88a1fede90e3ace4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
#####第二种方式(方法里传参)
```http://127.0.0.1:8000/usercollectAPI2/138/ 输入用户id即可```
#####url设置
```url(r'^usercollectAPI2/( ?P<userID>\d+)/$','personalRecommend.views.usercollectAPI2',
name='usercollectAPI2')```
```(?P<userID>\d+)```这个正则表达式的意思是将传入的一位或者多位数字作为参数传递到views中的detail作为参数, 其中```?P<userID>```定义名称用于标识匹配的内容
#####方法
def usercollectAPI2(request,userID):
try:
userID = int(userID)
except ValueError:
raise Http404()
userCollectList=serializers.serialize("json",UserProgramCollect.objects.filter(user_id = userID))
return HttpResponse(userCollectList, content_type='application/json; charset=utf-8')```