Django Rest Framework: what happened to my default Renderer? - json

I would like calls to /contacts/1.json to return json, 1.api to return browsableAPI, and calls with format=None aka /contacts/1/ to return a template where we call render_form. This way end-users can have pretty forms, and developers can use the .api format, and ajax/apps etc use .json. Seems like a common use case but something isn't clicking for me here in DRF...
Struggling with how DRF determines the Renderer used when no format is given. I found and then lost some info here on stack exchange that basically said to split the responses based on format. Adding the TemplateHTMLRenderer caused all sorts of pain. I had tried to split based on format but that is giving me JSON error below.
I don't understand the de facto way to define what renderer should be used. Especially when no format is provided. I mean, it "just works" when using Response(data). And I can get the TemplateHTMLRenderer to work but at the cost of having no default Renderer.
GET /contacts/1/ Gives the error:
<Contact: Contact object> is not JSON serializable
Using this code:
class ContactDetail(APIView):
permission_classes = (permissions.IsAuthenticatedOrReadOnly,
IsOwnerOrReadOnly,)
queryset = Contact.objects.all()
renderer_classes = (BrowsableAPIRenderer, JSONRenderer, TemplateHTMLRenderer,)
"""
Retrieve, update or delete a contact instance.
"""
def get_object(self, pk):
try:
return Contact.objects.get(pk=pk)
except Contact.DoesNotExist:
raise Http404
def get(self, request, pk, format=None):
contact = self.get_object(pk)
serializer = ContactSerializer(contact)
if format == 'json' or format == "api":
return Response(serializer.data)
else:
return Response({'contact': contact, 'serializer':serializer}, template_name="contact/contact_detail.html")
But GET /contacts/1.json , 1.api, or 1.html ALL give me the correct output. So it seems that I have created an issue with the content negotiation for the default i.e. format=None
I must be missing something fundamental. I have gone through the 2 tutorials and read the Renderers docs but I am unclear on what I messed up here as far as the default. I am NOT using the DEFAULT_RENDERERS in settings.py, didn't seem to make a difference if in default or inside the actual class as shown above.
Also if anyone knows a way to use TemplateHTMLRenderer without needing to switch on format value, I'm all ears.
EDIT: IF I use
if format == 'json' or format == "api" or format == None:
return Response(serializer.data)
else:
return Response({'contact': contact, 'serializer':serializer},
Then I am shown the browsable API by default. Unfortunately, what I want is the Template HTML view by default, which is set to show forms for end users. I would like to keep the .api format for developers.

TL; DR: Check the order of your renderers - they are tried in order of declaration until a content negotiation match or an error occurs.
Changing the line
renderer_classes = (BrowsableAPIRenderer, JSONRenderer, TemplateHTMLRenderer, )
to
renderer_classes = (TemplateHTMLRenderer, BrowsableAPIRenderer, JSONRenderer, )
Worked for me. I believe the reason is because the content negotiator starts at the first element in the renderer classes tuple when trying to find a renderer. When I have format==None, I'm thinking there is nothing else for DRF to go on, so it assumes I mean the "default" which seems to be the first in the tuple.
EDIT: So, as pointed out by #Ross in his answer, there is also a global setting in the settings.py for the project. If I remove my class level renderer_classes declaration and instead use this in settings.py
# ERROR
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.BrowsableAPIRenderer',
'rest_framework.renderers.JSONRenderer',
'rest_framework.renderers.TemplateHTMLRenderer',
)
}
Then I get a (different) JSON error. However, as long as
'rest_framework.renderers.BrowsableAPIRenderer',
is not listed first, for example:
# SUCCESS, even though JSON renderer is checked first
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
'rest_framework.renderers.TemplateHTMLRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
)
So if we hit BrowsableAPIRenderer before we try TemplateHTMLRenderer then we get an error - whether or not we are relying on renderer_classes or DEFAULT_RENDERER_CLASSES. I imagine it passes through JSONRenderer gracefully but for whatever reason BrowsableAPIRenderer raises an exception.
So I have simplified my view code after analyzing this...
def get(self, request, pk, format=None):
contact = self.get_object(pk)
serializer = ContactSerializer(contact)
if format == None:
return Response({'contact': contact, 'serializer':serializer}, template_name="contact/contact_detail.html")
else:
return Response(serializer.data)
..which better reflects what I was originally trying to do anyway.

When I look at the source code, the priority seems to be the order of the renderers specified in the DEFAULT_RENDERER_CLASSES parameter in settings.py:
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
'rest_framework.renderers.TemplateHTMLRenderer',
),
'DEFAULT_PARSER_CLASSES': (
'rest_framework.parsers.JSONParser',
'rest_framework.parsers.TemplateHTMLRenderer',
)
}
So, if you specify a bunch of renderer classes, the first renderer that is valid will be selected based on if it is valid for the request given the .json/.api/.html extension and the Accept: header (not content-type, as I said in the comment on your question).

Related

Return JsonResponse in Class-based CreateView

I'm using curl to submit form data to my website.
curl -F some_file=#file.txt -F name=test_01 https://localhost:8000
It's not an API but I have a requirement for a single endpoint that behaves as an API. I'm a little out of my depth here, so I'm hoping someone can help me.
I've got the model set up and working and the CreateView, as well:
class CreateFile(CreateView):
model = SomeFile
fields = ['name', 'some_file', . . .]
When I send a POST request with curl as above to the specified URL (/file/request), the object is created in the DB and I get a response (eg, /thanks now which is an HTTP response from template view). But since a non-browser will be sending this request, I was hoping to respond with some JSON. Maybe with the object's name, status, etc.
I've tried a few things with mixed results... For example, if I use View instead of CreateView, I can return JSON but I really like the ease and convenience of the CreateView CBV, so I'm hoping I can do what I want this way.
How can I do this? I found a SO question that gave some clues: How do I return JSON response in Class based views, instead of HTTP response
But this deals with the typical form/view model in the browser. If I have to override the post method, what's the best way to get the form data so I can create the object? Do I need a form class even though I'm not processing a rendered form?
I ended up going with something from the Django docs:
from django.http import JsonResponse
class JSONResponseMixin:
"""
A mixin that can be used to render a JSON response
"""
def render_to_json_response(self, context, **response_kwargs):
return JsonResponse(self.get_data(context), **response_kwargs)
def get_data(self, context):
return context
Then I used this in a DetailView, overriding both get and post methods.
class FileRequest(JSONResponseMixin, DetailView):
def get:
. . .
return self.render_to_response(response_data)
def post:
. . .
return self.render_to_response(response_data)
def render_to_response(self, context, **response_kwargs):
return self.render_to_json_response(context, **response_kwargs)

Import csv file in drf

I'm trying to create a view to import a csv using drf and django-import-export.
My example (I'm doing baby steps and debugging to learn):
class ImportMyExampleView(APIView):
parser_classes = (FileUploadParser, )
def post(self, request, filename, format=None):
person_resource = PersonResource()
dataset = Dataset()
new_persons = request.data['file']
imported_data = dataset.load(new_persons.read())
return Response("Ok - Babysteps")
But I get this error (using postman):
Tablib has no format 'None' or it is not registered.
Changing to imported_data = Dataset().load(new_persons.read().decode(), format='csv', headers=False) I get this new error:
InvalidDimensions at /v1/myupload/test_import.csv
No exception message supplied
Does anyone have any tips or can indicate a reference? I'm following this site, but I'm having to "translate" to drf.
Starting with baby steps is a great idea. I would suggest get a standalone script working first so that you can check the file can be read and imported.
If you can set breakpoints and step into the django-import-export source, this will save you a lot of time in understanding what's going on.
A sample test function (based on the example app):
def test_import():
with open('./books-sample.csv', 'r') as fh:
dataset = Dataset().load(fh)
book_resource = BookResource()
result = book_resource.import_data(dataset, raise_errors=True)
print(result.totals)
You can adapt this so that you import your own data. Once this works OK then you can integrate it with your post() function.
I recommend getting the example app running because it will demonstrate how imports work.
InvalidDimensions means that the dataset you're trying to load doesn't match the format expected by Dataset. Try removing the headers=False arg or explicitly declare the headers (headers=['h1', 'h2', 'h3'] - swap in the correct names for your headers).

Passing a map to Grails JSON view gson template can't pass values everything is null

The following action:
def addMembers(){
Map result = [message:"successful"]
try {
def group = Group.get(params.id)
def json = request.JSON
def users = json.users.collect{Usr.get(it.id)}
result.members = groupService.addMembers(group,users)
}catch(Exception e){
message = "Exception $e"
result.message = message
response.setStatus(hsr.SC_METHOD_NOT_ALLOWED)
}
respond result, [model:[result:result]]
}//eo addMember
In conjunction with the following addMembers.gson file
model {
Map result
}
json{
message result.message
members g.render(template:"simpleMember", collection: result.members, var:'member')
}
Gets a null pointer exception:
java.lang.NullPointerException: Cannot get property 'message' on null object
I have things working well in other actions where I respond domain objects but the client side needs a message if I catch an exception and it really didn't seem worth it to create an arbitrary pogo when [message:"",members:[]] could do the same amount of work as an arbitrary extra file and an extra 5-10 lines of code.
Update 1
I tried replacing Map with an arbitrary ResultHolder class to placate any strict typing that might have been in play.
That didn't help
Update 2
In my .gson file I replaced
json{
with
json g.render(result){
And that gets me null output instead of null pointers.
Equally unacceptable.
Update 3
In order to try to assess how to interact with gson template without depending on ajax posts and database interaction I make the following arbitrary action:
def jsonDbug(){
def result = [message:"hi"]
respond result, [model:[jsonDbug:result]]
}
and an arbitrary gson file:
model{
Map jsonDebug
}
json{
says jsonDebug.message
}
This allows me to make changes faster to see what is going wrong.
I'm trying calling in other ways except respond now but nothing works.
It's as if JSON views are strictly for domain objects and nothing else.
It turns out that I wasn't that very far off when I started.
The problem was:
respond result, [model:[result:result]]
Which is the predominant example used at http://views.grails.org/latest/
When I changed to:
render(view:"addMembers", model:[result:result])
It worked exactly how I wanted it to.

DjangoRestFramework - How to send JSON object and HTML Template to the front-end at the same time?

What I want is, if a user goes to this URL:
/user/22
then the front-end should render an HTML page for the user who's pk value is 22. This is my URLs.py:
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
urlpatterns = [
url(r'^user/(?P<pk>[0-9]+)$', views.UserPageView.as_view()),
url(r'^', include(router.urls)),
]
And this is UserPageView:
class UserPageView(generics.RetrieveAPIView):
queryset = User.objects.all()
renderer_classes = (TemplateHTMLRenderer,)
def get(self, request, *args, **kwargs):
self.user = self.get_object()
return Response({'user': self.user}, template_name='user.html')
And this is my UserViewSet:
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
permission_classes = (IsCreationOrAuthenticated, IsWatchOrOwnerOrReadOnly,)
Assuming 'user' is a JS object, I plan on using my DRF API by sending a get request using AngularJS to the following URL:
("/users/" + user.pk) // which leads to UserViewSet (which serializes the user object)
to get the information for that specific user. However, user is not a JSON object, it is a Django variable which I can use in the template using the Django template tags, like so: {{ user }}.
How do I get my DRF view to return JSON to the HTML template as well? What's the best way for me to do this? Thanks in advance!
I'm not exactly sure if there's any chance that it will work the way you're currently doing at, as the first met URL will always direct you to the UserPageView.
However, perhaps you should take a look at DRF's format_suffixes.
With an additional format you can specify if you want to get the JSON or HTML representation of the user object.

Include JSON from Django Rest Framework in an HTML template

I am trying to do something very simple but haven't found how to do it yet.
I have a model and an endpoint returning a JSON array reprenseting the instances of this model with Django Rest Framework. I want to include the JSON in an HTML template (for SEO and for fast initial data loading). Something like
<script>
var data = {% json_from_django_rest_framework "mymodel" %};
</script>
Is there an easy way to do this? Should I just go a different way?
Another way of doing this, which gets around rendering the view.
In your views.py;
class FooDetailView(DetailView):
model = Foo
template_name = 'foo/detail.html'
def get_context_data(self, **kwargs):
bars = []
for bar in self.object.bars.all():
bars.append(BarSerializer(bar).data)
kwargs['bars'] = JSONRenderer().render(bars)
return super(FooDetailView, self).get_context_data(**kwargs)
And in your template;
<script>
var bars = {{ bars|safe }};
</script>
It should really go without saying that you should pay attention to potential performance issues with this approach, ie.. perhaps it's best to paginate bars.
As discussed in the comments, here is an example of how to reuse the result from your api endpoint in a normal Django view by using Django's resolve function.
views.py
import json
from django.core.urlresolvers import resolve
from django.views.generic.base import View
class FooView(View):
def get(self, request):
# optional stuff in your view...
##
# Resolving another Django view programmatically
##
rev = '/path/to/api/endpoint/' # or use reverse()
view, vargs, vkwargs = resolve(rev)
vkwargs['request'] = request
res = view(*vargs, **vkwargs)
c = Context({
'data': json.dumps(res.data)
})
# Now the JSON serialized result from the API endpoint
# will be available in the template variable data.
return render(request, 'my-app/my-template.html', c)
my-template.html
<script>
var data = {{ data }};
</script>
Note 1: Instead of hardcoding the path in rev = '/path/to/api/endpoint/' it is better to reverse() the url, but I left it out to remove that as a source for errors. If you are going this direction, here is a list of the default url names provided by DRF
Note 2: The snippet would benefit from exception handling, like making sure that res returned 200, has the data property, etc.