2013-08-16 19 views
11

Come passare la risposta django restframework per qualsiasi richiesta in html. Esempio: un elenco che contiene oggetti e html be articles.html.Come passare la risposta del framework di django in html?

ho cercato utilizzando Risposta quadro riposo:

data= {'articles': Article.objects.all() } 
return Response(data, template_name='articles.html') 

sto ottenendo questo errore:

""" AssertionError at /articles/ 

.accepted_renderer not set on Response """ 

dove ho sbagliato, per favore mi suggeriscono.

+0

http://django-rest-framework.org/api-guide/renderers.html Cercare HTML Renderer – Denis

+0

Il link di cui sopra non funziona, ma questo dovrebbe: http: //www.django -rest-framework.org/api-guide/renderers/ – rschwieb

risposta

33

Se si tratta di una visione funzione di base, è reso necessario utilizzare un decoratore @api_view per visualizzare correttamente. Ho visto questo particolare errore accadere per questo motivo esatto (mancata dichiarazione della vista API nelle viste basate sulla funzione).

from rest_framework.decorators import api_view 
# .... 

@api_view(['GET', 'POST', ]) 
def articles(request, format=None): 
    data= {'articles': Article.objects.all() } 
    return Response(data, template_name='articles.html') 
1

vi siete persi TemplateHTMLRenderer decoratore:

 
@api_view(('GET',)) 
@renderer_classes((TemplateHTMLRenderer,)) 
def articles(request, format=None): 
    data= {'articles': Article.objects.all() } 
    return Response(data, template_name='articles.html') 
+0

Continuo a ricevere un TypeError: non serializzabile in JSON. Tutto sembra essere esattamente come suggerisci. – Bobort

5

Nel mio caso solo dimenticato di impostare @api_view ([ 'mettere']) sulla funzione di visualizzazione.

So,
.accepted_renderer
The renderer instance that will be used to render the response not set for view.
Set automatically by the APIView or @api_view immediately before the response is returned from the view.

Problemi correlati