Django身份验证不在生产中工作,但在本地工作。模型不使用OnetoOnefield

时间:2017-02-07 12:29:10

标签: python django azure heroku

超级用户创建工作正常,我可以在管理面板中访问它们。然而,当用户创建一个帐户时,他们会被添加为用户但不会传播到FacebookProfile模型中(因此我无法添加他们的id,webpull等...)(他们在sqllite3的开发中传播得很好)

我在azure上使用mysql。

models.py

from django.db import models

from django.forms import ModelForm

from django.contrib.auth.models import User

from django import forms


class FacebookProfile(models.Model):
     user = models.OneToOneField(User, on_delete = models.CASCADE)

     id = models.PositiveIntegerField(primary_key = True)
     webpull= models.CharField(max_length =1000, null = True)
     score = models.PositiveIntegerField(default = 0, editable = True)
     times_called = models.PositiveIntegerField(default = 0, editable = True)
     level = models.FloatField(default = 0, editable =True)

我的Facebook用户表单 forms.py

from django import forms
from django.contrib.auth.models import User

from .models import FacebookProfile


#facebook api linkage
class FacebookUserForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ('username', 'first_name', 'last_name',)


class FacebookProfileForm(forms.ModelForm):
    class Meta:
        model = FacebookProfile
        fields = ('id', 'year_formed', 'webpull',)

我的自定义身份验证系统 auth_backend.py

from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.models import User

class PasswordlessAuthBackend(ModelBackend):

    def authenticate(self, username = None):
        try:
            return User.objects.get(username = username)
        except:
            return None

我的观点来自于我从Facebook API处理输入的参数并将其存储在模型中 views.py

if request.method == "POST":
        print ('ive been posted')
        first_name = request.POST.get('first_name')
        last_name = request.POST.get('last_name')
        username = request.POST.get('username')
        print (first_name, last_name, username)

        facebook_user = FacebookUserForm(data=request.POST)
        facebook_profile = FacebookProfileForm()

        has_account = authenticate(username = username)
        print (has_account)

        if has_account:
            print ('this has account')
            login(request, has_account)
            return HttpResponseRedirect('/home/')
        else:

            id_ = request.POST.get('username')
            webpull = request.POST.get('webpull')


            print (id_, birthday, webpull)

            print (facebook_user)

            user = facebook_user.save()
            profile = facebook_profile.save(commit = False)
            profile.user = user
            profile.webpull = webpull
            profile.id = id_


            profile.save()



            now_has_account = authenticate(username = username)
            login(request, now_has_account)

            return HttpResponseRedirect('/home/')

我的生产settings.py

  AUTHENTICATION_BACKENDS = (

        'authentication.auth_backend.PasswordlessAuthBackend',

        ('django.contrib.auth.backends.ModelBackend'),
    )

我没有密码的身份验证方法。

现在是模型中OnetoOneField的问题?我不知道为什么一切都在本地工作,但当我推送到heroku时,唯一不起作用的是将用户传播到FacebookProfile。

0 个答案:

没有答案