2015-04-23 65 views
0

我需要創建一個API,它將採用POST數據中的application/x-www-form-urlencoded數據並返回HTML內容作爲響應。Tastypie:在API響應中返回html內容

現在序列化urlencoded的數據我打電話從資源以下串行:

class urlencodeSerializer(Serializer): 
formats = ['json', 'jsonp', 'xml', 'yaml', 'html', 'plist', 'urlencode'] 
content_types = { 
    'json': 'application/json', 
    'jsonp': 'text/javascript', 
    'xml': 'application/xml', 
    'yaml': 'text/yaml', 
    'html': 'text/html', 
    'plist': 'application/x-plist', 
    'urlencode': 'application/x-www-form-urlencoded', 
    } 
def from_urlencode(self, data,options=None): 
    """ handles basic formencoded url posts """ 
    qs = dict((k, v if len(v)>1 else v[0]) 
    for k, v in urlparse.parse_qs(data).iteritems()) 
    return qs 

def to_urlencode(self,content): 
    pass 

要指定我已經添加在資源此功能的響應格式:

def determine_format(self, request): 
     return 'text/html' 

現在,當我試圖輸出如下HTML響應:

data = "<html><h1>Hello</h1></html>" 
return HttpResponse(data, content_type='text/html', status=200) 

I我得到以下錯誤:

Sorry, not implemented yet. Please append "?format=json" to your URL. 

有人可以建議我這個代碼有什麼問題,以及如何實現給定的要求。

+0

  • 添加下面的方法你試圖啓用HTML只是整個系統的一個資源? – Railslide

  • +0

    @Railslide只爲一個資源 –

    回答

    0

    我已經找到了答案,雖然不知道它的正確方式tastypie做到:

    1. 正常返回從資源捆綁包響應。在urlencodeSerializer

      def to_html(self,bundle, options): 
          return prepare_html_from_bundle_data(bundle.data)