2017-10-11 64 views
1

嗨,我真的很感激,如果有人可以請粘貼代碼在那裏爲他們的Django項目創建Facebook登錄,無論它是一個單獨的應用程序與否,有幾個解釋。拉用戶名,電子郵件和個人資料圖片。謝謝執行Facebook登錄與Django

+0

有大量可用於Django的社會身份驗證庫。如果你谷歌它,你會發現一些在github上 –

+1

請參閱此[鏈接](https://simpleisbetterthancomplex.com/tutorial/2016/10/24/how-to-add-social-login-to-django.html)可能幫助你 –

+0

謝謝我希望不要使用第三方應用程序。你是否使用過社交認證應用django或django-allAuth? – Josh

回答

1

它花了我一個星期,但我實施Facebook登錄困難的方式。如果您不希望您的網站上的第三方應用程序(更安全和值得信賴的用戶),這裏的步驟:

  1. 獲取這裏的FB登錄按鈕:(https://developers.facebook.com/docs/facebook-login/web/login-button)。在複製代碼之前,您可以更改按鈕的設置。

  2. 在此處獲取javascript插件(https://developers.facebook.com/docs/facebook-login/web)。我建議複製的例子並修改如下:

的Javascript:

if (response.status === 'connected') { 
     // Logged into your app and Facebook. 
    FB.api('/me', {fields: 'name, email'}, function(response) { 
     console.log('Successful login for: ' + response.name); 
     document.getElementById("your_name2").value = response.name; 
     document.getElementById("your_email").value = response.email; 
     document.getElementById("myForm").submit(); 
     document.getElementById('status').innerHTML = 
     'Thanks for logging in, ' + response.name + response.email + '!';}); 

一旦登錄和「連接」你需要改變你撥打信息。加上你需要的{fields...}。保持日誌,看它是否工作。

  1. 將2中拉入的信息提交到隱藏窗體中,以便將其發送到視圖和模型。這裏是形式(hellls只是默認值):

表單模板:

<form action="{% url 'facebooklogin:register' %}" method="post" style="display: none;" id="myForm"> 
    {% csrf_token %} 
    <label for="your_name">Your name: </label> 
    <input id="your_name2" type="text" name="your_name" value="helllllls"> 
    <input id="your_email" type="text" name="your_email" value="helllllls"> 
    <input type="submit" value="OK"> 
</form> 
  • 設置表單,模型和視圖處理的信息,你想。獲取個人資料圖片,但只需添加一個ImageField即可。
  • 網址:

    url(r'^registerfb/$', views.get_name, name='register') 
    

    VIEW:

    def get_name(request): 
        # if this is a POST request we need to process the form data 
        if request.method == 'POST': 
         # create a form instance and populate it with data from the request: 
         form = NameForm(request.POST) 
         # check whether it's valid: 
         if form.is_valid(): 
          # process the data in form.cleaned_data as required 
          logger.error('Form is valid and running') 
          logger.error(request.POST.get('your_name')) 
          logger.error(request.POST.get('your_email')) 
          # redirect to a new URL: 
    
          return HttpResponseRedirect('/thanks/') 
    
        # if a GET (or any other method) we'll create a blank form 
        else: 
         form = NameForm() 
    
        return render(request, 'facebooklogin/name.html', {'form': form}) 
    

    FORM:

    class NameForm(ModelForm): 
        class Meta: 
         model = FBUser 
         fields = ['your_name', 'your_email',] 
    
    +0

    我在使用django-social包幾個月。大約兩天前它停止工作。谷歌搜索和升級後,它仍然無法正常工作。我會試試這個;謝謝,我希望它能起作用。 –

    +0

    當然,我可能已經制作了幾個mod,但是完美地工作。 – Josh

    +0

    非常感謝喬希 –