2017-09-05 83 views
0

我想一個API添加到使用瓶我的Django應用程序,該API的目的是返回一些JSON數據,如果電子郵件地址和密碼通過POST請求是正確的:django應用程序內部的Flask RESTful API錯誤:視圖mysite.core.views.getallitems沒有返回HttpResponse對象。它返回無不是

網址。潘岳:

urlpatterns = [ 
    url(r'^getallitems', core_views.getallitems, name='getallitems'), 
] 

views.py:

@csrf_exempt 
def getallitems(request): 
    def post(self): 
     try: 
      data = { 
       'name': 'Vitor', 
       'location': 'Finland', 
       'is_active': True, 
       'count': 28 
      } 

      return JsonResponse(data, status=201) 

     except Exception as e: 
      return HttpResponse(status=201) 

錯誤:

Request Method: POST 
Request URL: http://127.0.0.1:8000/getallitems 
Django Version: 1.11.3 
Exception Type: ValueError 
Exception Value:  
The view mysite.core.views.getallitems didn't return an HttpResponse object. It returned None instead. 
Exception Location: /usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py in _get_response, line 198 
Python Executable: /usr/bin/python 
Python Version: 2.7.12 
Python Path:  
['/home/gaby/django projects/simple-signup-master/profile-model', 
'/usr/local/lib/python2.7/dist-packages/virtualenv-15.1.0-py2.7.egg', 
'/usr/lib/python2.7', 
'/usr/lib/python2.7/plat-x86_64-linux-gnu', 
'/usr/lib/python2.7/lib-tk', 
'/usr/lib/python2.7/lib-old', 
'/usr/lib/python2.7/lib-dynload', 
'/home/gaby/.local/lib/python2.7/site-packages', 
'/usr/local/lib/python2.7/dist-packages', 
'/usr/lib/python2.7/dist-packages', 
'/usr/lib/python2.7/dist-packages/PILcompat', 
'/usr/lib/python2.7/dist-packages/gtk-2.0', 
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client'] 
Server time: Tue, 5 Sep 2017 07:25:25 +0000 

我試圖在HTTP響應返回JSON數據,他們都給我發現StackOverflow上相關的文章同樣的錯誤,但他們沒有解決我的問題。

回答

0

你需要從函數返回getallitems響應這樣

@csrf_exempt 
def getallitems(request): 
    def post(self): 
     try: 
      ...... 
     except: 
      ..... 
    return post() 
+0

它解決了這一問題,謝謝@marni –

+0

歡迎您 – AndMar

相關問題