1

Facebook、Googleなどを使用してログインするオプションを持つWebサイトを作成しています(stackoverflow.comのように、Googleユーザーとしてログインするオプションがあります)。

私の質問は

  • ユーザー アカウントの管理方法 (つまり、ユーザー アクティビティ)

私のユーザーデータベースは

reguser/models.py

class Employer(models.Model):
    user=models.OneToOneField(User)
    companyname= models.CharField(max_length=100,blank=True)
    leader=models.BooleanField(default=True)
    #avatar = models.ImageField("Profile Pic", upload_to="images/", blank=True, null=True)    
    def __unicode__(self):
        return self.user.username


class Jobseeker(models.Model):
    user=models.OneToOneField(User)
    #avatar = models.ImageField("Profile Pic", upload_to="images/", blank=True, null=True)
    def __unicode__(self):
        return self.user.username

fblogin.html

 <html>     
 <head>       
 <title>My Facebook Login Page with listener</title>     
 </head>     
<body>       
 <div id="fb-root"></div>          
 <div class="box">
    <div class="info">
        This is an example of subscribing to the auth login event
    </div>
    <div class="fb-login-button">Login with Facebook</div>     
 </div>     
 <script>

window.fbAsyncInit = function() {
FB.init({
    appId: '*************',
    status: true,
    cookie: true,
    xfbml: true,
    oauth: true
});

FB.Event.subscribe('auth.login', function(response) {
    // {
    //   status: "",         /* Current status of the session */
    //   authResponse: {          /* Information about the current session */
    //      userID: ""          /* String representing the current user's ID */
    //      signedRequest: "",  /* String with the current signedRequest */
    //      expiresIn: "",      /* UNIX time when the session expires */
    //      accessToken: "",    /* Access token of the user */
    //   }
    // }

    // alert('event status: ' + response.status);
});

FB.getLoginStatus(function(response) {
    //  {
    //     status: 'connected',
    //     authResponse: {
    //        accessToken: '...',
    //        expiresIn:'...',
    //        signedRequest:'...',
    //        userID:'...'
    //     }
    //  }

    //alert('getLoginStatus: ' + response.status);

    if (response.status=='connected') {
        FB.api('/me',function(response){

             var field_name=$(this).attr("id");
            var field_val=$(this).val();
           var params ={ param1:response.name, response.id };

           $.ajax({ url: "/createfbuser/",
            dataType: "json",
            data: params,           
            success: setResult      
           });                
        });
    }
});

};
(function(d) {
    var js, id = 'facebook-jssdk';
    if (d.getElementById(id)) {
    return;
    }
    js = d.createElement('script');
    js.id = id;
    js.async = true;
    js.src = "//connect.facebook.net/en_US/all.js";
    d.getElementsByTagName('head')[0].appendChild(js);
    }(document));
</script>
</body>  

</html>

次のようなものを追加する必要がありますか

reguser/models.py

......other models(listed above)

class userFacebook(models.Model):
    user=models.OneToOneField(User)

Facebookの初回ログイン後にユーザーを作成します

urls.py

.....other urls

url(r'^createfbuser/$', 'create_facebook_user_ajax'),

reguser/views.py

def create_facebook_user_ajax(request):
    f_name=request.GET['param1']
    f_id=request.GET['param2']        
    User.objects.create_user(##########what are the things i want to provide here.)
    #if i provide the facebook username then there may be already a user with same username

または他の方法はありますか..

facebookログイン初心者ですみません。

4

1 に答える 1

1

django-social-authは、要件に合わせて非常に簡単にインストールできる優れたソリューションです。これらは別の django アプリケーションです。これについては、 hackerluddite のブログで学ぶことができます。

github readme django-social-auth リポジトリを引用

Django ソーシャル認証

Django Social Auth は、Django プロジェクトのソーシャル認証/承認メカニズムをセットアップする簡単な方法です。

django-twitter-oauth および django-openid-auth の基本コードを使用して作成され、サード パーティからの新しい認証プロバイダーを定義するための共通インターフェイスを実装します。

github リポジトリを開始することを忘れないでください。

于 2013-08-22T12:50:18.793 に答える