4

I'm trying to get xml format in Django Rest FrameWork,I tried the tutorial provided by Django Rest Framework, I'm new to django, I did the following.

settings.py

 INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'rest_framework',
        'books',
        'users',
    ]

urls.py

from django.conf.urls import url
from django.contrib import admin
from books.views import *
from users.views import *
from rest_framework.urlpatterns import format_suffix_patterns

    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^books/all/$', all_books),
        url(r'^user/', get_user)
    ]

    urlpatterns = format_suffix_patterns(urlpatterns, allowed=['json', 'html','xml'])

views.py

from rest_framework.response import Response
from rest_framework.decorators import api_view
from books.serializers import *
from books.models import *

# Create your views here.

@api_view(['GET'])
def all_books(request):
    books = Book.objects.all()
    serializers = BookSerializer(books,many=True)
    return Response(serializers.data)

When I try to access the xml data, I Get this error by doing ?format=xml

{"detail":"Not found."}

the tutorial link http://www.django-rest-framework.org/api-guide/format-suffixes/

2

2 Answers 2

6

Actually your settings.py is missing the xml parser config.

  1. install rest_framework_xml : pip install djangorestframework-xml
  2. Update INSTALLED_APPS in settings.py
INSTALLED_APPS = [
  'django.contrib.admin',
  'django.contrib.auth',
  'django.contrib.contenttypes',
  'django.contrib.sessions',
  'django.contrib.messages',
  'django.contrib.staticfiles',
  'rest_framework',
  'rest_framework_xml',
  'books',
  'users',
]
  1. Add xml parser in settings.py:
REST_FRAMEWORK = {
  'DEFAULT_PARSER_CLASSES': (
    'rest_framework_xml.parsers.XMLParser',
  ),
    'DEFAULT_RENDERER_CLASSES': (
    'rest_framework_xml.renderers.XMLRenderer',
  ),
}
Sign up to request clarification or add additional context in comments.

7 Comments

I got this error ImportError: Could not import 'rest_framework_xml.renderers.XMLRenderer' for API setting 'DEFAULT_RENDERER_CLASSES'. ImportError: No module named 'rest_framework_xml'.
you have to install rest_framework_xml as the error explains. I will update the answer to be a complete one.
did you follow my new update? you have to add it to the list of INSTALLED_APPS as mentioned above. It tested it locally and working with the config above.
Yes, I'm getting ImportError: No module named 'rest_framework_xml', but it's already installed and I added the 'rest_framework_xml'.
That's definitely weird. Check your environment.
|
0

It is adviceable that you use virtual environment. Check how to use pipenv here

2 Comments

This feels much more like a comment to the answer given than an actual answer to the question
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.