1
我有點明白登錄如何與TastyPie和Django的,但有一件事困擾着我。 一旦像如何通知應用程序憑據成功在Django/Tastypie/REST
{"username": username,"password":password} or "apikey":apikey
通過的HttpRequest和必要的身份驗證完成後通過API發送,如何將移動應用程序知道它是成功的? 我將使用什麼代碼來告訴本機應用程序認證是否成功,從而可以繼續。
我發現這個代碼在這裏SO和理解的,但在非瀏覽器應用程序的情況下部分,它怎麼會知道?
類UserResource(ModelResource):
class Meta:
queryset = User.objects.all()
resource_name = 'user'
allowed_methods = ['post']
def prepend_urls(self):
return [
url(r"^user/login/$", self.wrap_view('login'), name="api_login"),
url(r"^user/logout/$", self.wrap_view('logout'), name='api_logout'),
]
def login(self, request, **kwargs):
self.method_check(request, allowed=['post'])
data = self.deserialize(request, request.raw_post_data, format=request.META.get('CONTENT_TYPE', 'application/json'))
username = data.get('username', '')
password = data.get('password', '')
user = authenticate(username=username, password=password)
if user:
if user.is_active:
login(request, user)
return self.create_response(request, {
'success': True
})
else:
return self.create_response(request, {
'success': False,
'reason': 'disabled',
}, HttpForbidden)
else:
return self.create_response(request, {
'success': False,
'reason': 'incorrect',
}, HttpUnauthorized)
def logout(self, request, **kwargs):
self.method_check(request, allowed=['post'])
if request.user and request.user.is_authenticated():
logout(request)
return self.create_response(request, { 'success': True })
else:
return self.create_response(request, { 'success': False }, HttpUnauthorized)
編輯:
問不同...... 我怎樣才能通知該獨立python腳本:
url="http://127.0.0.1:8000/articles/api/article/"
data={
'title':'Tastypie Madness',
'body':'First Restful client. Enjoy',
'pub_date':str(time.strftime('%Y-%m-%d %H:%M:%S')),
}
files= {'thumbnail': open('car.jpg', 'rb')}
req = requests.post(url, data=data, files=files)
認爲這是對象創建,通過這個API成功保存:
類ArticleResource(ModelResource):
class Meta:
queryset = Article.objects.all()
resource_name = 'article'
filtering = {'title': ALL}
authorization=Authorization()
def deserialize(self, request, data, format=None):
if not format:
format = request.META.get('CONTENT_TYPE', 'application/json')
if format =='application/x-www-form-urlencoded':
data = request.POST.copy()
photo = Article()
photo.title = request.POST.get('title')
photo.body=request.POST.get('body')
photo.pub_date = request.POST.get('pub_date')
photo.save()
return data
if format.startswith('multipart'):
data = request.POST.copy()
photo = Article()
photo.thumbnail = request.FILES['thumbnail']
photo.title = request.POST.get('title')
photo.body=request.POST.get('body')
photo.pub_date = request.POST.get('pub_date')
photo.save()
# ... etc
return data
return super(ArticleResource, self).deserialize(request, data, format)
# overriding the save method to prevent the object getting saved twice
def obj_create(self, bundle, request=None, **kwargs):
pass
和打印 「成功發佈」 在屏幕上。
你如何獲取服務器的答案在你的客戶? – spectras 2015-02-07 16:04:02
@spectras是的,這就是我問的。我必須手動請求嗎? – aggolb 2015-02-07 16:06:39
那麼,你的客戶端應用程序將提交請求,就像你寫的一樣。服務器將執行一些操作,然後發回一個答案,讓客戶端應用程序解析。示例代碼已經這樣做了,唯一的問題是:你有什麼樣的客戶端應用程序?基於瀏覽器的jQuery的?一些手機框架?你需要知道如何使用你的特定工具包得到答案。這是一個客戶端問題,而不是服務器問題。服務器已經在發送答案。 – spectras 2015-02-07 16:16:06