エラー: /login_or_signup キー 'name' で MultiValueDictKeyError が見つかりません
行番号やページについて言及せず、Djangoセットアップファイルのページのみを言及します(私のコードではありません)
iOS コードのこのブロックは失敗します
//Now pass this to our login
NSURL *url = [NSURL URLWithString:BASE_URL];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
name, @"name",
email, @"email",
fbid, @"fbid",
nil];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"/login_or_signup/" parameters:params];
NSLog(@"PARAMS %@", params);
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
//do something
}failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
// your failure code here
NSLog(@"FAILED %@", JSON);
}]
ログインまたはサインアップ ビュー コード:
@csrf_exempt
def login_or_signup(request):
user = False;
username = False;
email = "tmp@app.com"
password = "tmppass"
displayname = request.POST['name']
fbid = request.POST['fbid']
#Do we already have this user?
if "email" in request.POST:
email = request.POST['email']
username = email
try:
user = User.objects.get(email=email)
except User.DoesNotExist:
pass
if fbid and not user:
username = fbid
try:
user_profile = UserProfile.objects.get(fbid=fbid)
user = user_profile.user
except UserProfile.DoesNotExist:
pass
if not user:
user = User.objects.create_user(fbid, email, password)
#tastypie api_key
api = ApiKey.objects.get(user=user)
#Update user_profile:
user_profile = user.get_profile()
user_profile.username = displayname
user_profile.fbid = fbid
user_profile.save()
dic = list()
dic.append({
'username':user_profile.username,
'user_profile_id':user_profile.id,
'fbid':fbid,
'api_username':user.username,
'api_key':api.key
})
return HttpResponse(simplejson.dumps(dic, cls=DjangoJSONEncoder), mimetype='application/json')
いくつかの便利な iOS コード
- (void)request:(FBRequest *)request didLoad:(id)result {
//Loading details about this FB user. Send the details to Web App and create and/or login this user.
NSLog(@"FACEBOOK RESULT %@ ", result);
NSString * fbid = [result objectForKey:@"id"];
NSString * email = [result objectForKey:@"email"];
NSString * name = [result objectForKey:@"name"];
[self loginOrSignup:name email:email fbid:fbid];
}
また、上記の NSLog が機能することにも注意してください。ID、電子メール、名前などを確認できます。
AFNetworking 経由で URL に接続する loginorsignup iOS メソッドに送信されます。