Commit 8ce68bf5 authored by Emma's avatar Emma

🎨 Used DRF

parent 2424e193
from django.http import HttpResponse, JsonResponse
from django.views.generic import View
from rest_framework.response import Response
from rest_framework.views import APIView
from dicttoxml import dicttoxml
class WorldView(View):
class WorldView(APIView):
def get(self, request, *args, **kwargs):
response = {
return Response({
'message': 'HelloWorld',
}
if request.accepts('application/xml'):
return HttpResponse(dicttoxml(response, attr_type=False), content_type='application/xml')
if request.accepts('application/json'):
return JsonResponse(response)
return HttpResponse("This API only returns json or xml responses", status=406)
})
......@@ -31,6 +31,9 @@ ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.contenttypes',
'django.contrib.auth',
'rest_framework',
'hello',
]
......@@ -71,3 +74,10 @@ USE_TZ = False
# https://docs.djangoproject.com/en/dev/howto/static-files/
STATIC_URL = '/static/'
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': [
'rest_framework.renderers.JSONRenderer',
'rest_framework_xml.renderers.XMLRenderer',
]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment