2012-11-26 30 views
2

我一直在開發一個移動應用程序來訪問我的一個Django網站。我使用TastyPie完成了寧靜的API,並使用JQMobile開發了前端。我來到了我想要登錄用戶並訪問登錄用戶的部分。Django + JQMobile + PhoneGap

我已經做了很多的閱讀和搜索,但我仍然不確定什麼是最好的方法。理想情況下,我想用他們的用戶名和密碼登錄用戶,然後過濾該用戶返回的一些API數據(我可以通過TastyPie文檔進行處理)。

其他人是如何接近使用JQMobile和Django驗證用戶身份的。我也使用PhoneGap,因此如果需要,我可以將來自登錄的返回用戶信息存儲在本地存儲中。但我不太清楚如何將它們編碼在一起,以便在移動用戶使用應用程序時在django端提供request.user。

到目前爲止,我已經從用戶資源的TastyPie一側的UserResource中的另一個帖子中提出了這個問題,但用戶登錄後我不知道該怎麼辦。

class UserResource(ModelResource): 
    class Meta: 
     queryset = User.objects.all() 
     resource_name = 'user' 
     list_allowed_methods = ['get', 'post'] 

    def override_urls(self): 
     return [ 
      url(r"^(?P<resource_name>%s)/signin%s$" % 
       (self._meta.resource_name, trailing_slash()), 
       self.wrap_view('signin'), name="api_signin"), 
     ] 

    def signin(self, request, **kwargs): 
     self.method_check(request, allowed=['post']) 

     # Per https://docs.djangoproject.com/en/1.3/topics/auth/#django.contrib.auth.login... 
     username = request.GET['username'] 
     password = request.GET['password'] 
     user = authenticate(username=username, password=password) 

     if user is not None: 
      if user.is_active: 
       login(request, user) 
       return self.create_response(request, {'success': True}) 
      else: 
       # Return a 'disabled account' error message 
       return self.create_response(request, {'success': False}) 
     else: 
      # Return an 'invalid login' error message. 
      return self.create_response(request, {'success': False}) 

有沒有人有他們可以共享的任何代碼,或任何指針如何登錄用戶和維護他們的狀態?

乾杯, 本

回答

1

PhoneGap的實際上只是一個瀏覽器包裹在一些本地代碼,這意味着它具有相同的手段,堅持像正常的網頁瀏覽器會話做 - 餅乾!

每個發送到後端API的ajax請求都可以包含sessionid cookie,就像正常的GET請求一樣。 requst.user對象將在您的視圖中提供給您。

您不需要爲此構建任何特殊或使用localstorage。 verify is that your domain is whitelisted唯一的東西,所以你的應用程序可以訪問它。

+0

謝謝指出我在正確的方向 –