django-registration自动创建UserProfile [英] django-registration auto create UserProfile

查看:347
本文介绍了django-registration自动创建UserProfile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用django注册,我试图连接到它的信号来自动创建一个UserProfile。

I'm using django-registration and I'm trying to connect to its signals to automatically create a UserProfile.

信号定义:

from django.dispatch import Signal

# A new user has registered.
user_registered = Signal(providing_args=["user", "request"])

发送django注册:

Signal send by django-registration:

    def register(self, request, **kwargs):
    """
    Create and immediately log in a new user.

    """
    username, email, password = kwargs['username'], kwargs['email'], kwargs['password1']
    User.objects.create_user(username, email, password)

    # authenticate() always has to be called before login(), and
    # will return the user we just created.
    new_user = authenticate(username=username, password=password)
    login(request, new_user)
    signals.user_registered.send(sender=self.__class__,
                                 user=new_user,
                                 request=request)
    return new_user

我的信号连接:

from registration.signals import *
from core.models import UserProfile
from django.contrib.auth.models import User

def createUserProfile(sender, instance, **kwargs):
    UserProfile.objects.get_or_create(user=instance)

user_registered.connect(createUserProfile, sender=User)

不用说没有创建UserProfile。我在这里缺少什么?

Needless to say no UserProfile is being created. What am I missing here?

非常感谢!

编辑:将我的connect()及其相应的方法移动到一个model.py,但仍然没有运气。

I moved my connect() and its corresponding method to a model.py and still no luck.

新代码:

from django.db import models

from django.contrib import auth
from django.contrib.auth import login
from core.forms import AuthForm
from registration.signals import *
from django.contrib.auth.models import User


# Create your models here.

class UserProfile(models.Model) :
    user = models.ForeignKey(User, unique=True)

    def __unicode__(self):
        return self.user.username


def createUserProfile(sender, instance, **kwargs):
    print "creating profile"
    UserProfile.objects.get_or_create(user=instance)

user_registered.connect(createUserProfile, sender=User)

我正在使用Pycharm进行调试,最开始我的user_registered.connect()断点被命中。所以我假设connect()正在正确注册。但是,我仍然看不到正在运行的createUserProfile。还有其他我失踪的东西?

I'm using Pycharm to debug, and in the very beginning my breakpoint on user_registered.connect() is hit. So I assume that connect() is being registered correctly. However, I still don't see createUserProfile being run. Anything else I'm missing?

谢谢!

答案:我的连接和接收器代码是错误的。正确的代码:

ANSWER: Doh. My connect and receiver code was wrong. Correct code:

def createUserProfile(sender, user, request, **kwargs):
UserProfile.objects.get_or_create(user=user)

user_registered.connect(createUserProfile)

实现它在我在django-registration中阅读signals.py之后

Realized it after I read signals.py in django-registration

推荐答案

您需要在导入的模块中注册(连接)您的信号在服务器启动。您的文件,其中 user_registered.connect(createUserProfile,sender = User)生活在启动时可能不会导入。从django文档:

You need to register (connect) your signal in a module which is imported on server startup. Your file where user_registered.connect(createUserProfile, sender=User)lives is mot likely not imported on startup. From the django docs:


您可以随时随地放置信号处理和
注册码。
但是,您需要确保
的早期
导入的模块,以便在任何信号需要
之前,信号处理获得
注册被发送。这使您的应用程序的
models.py成为放置
注册信号处理程序的好地方。

You can put signal handling and registration code anywhere you like. However, you'll need to make sure that the module it's in gets imported early on so that the signal handling gets registered before any signals need to be sent. This makes your app's models.py a good place to put registration of signal handlers.

http://docs.djangoproject.com/en/dev/topics/信号/#连接接收器功能

所以你的自定义应用程序的models.py将是一个好的地方(或任何其他模块,绝对导入服务器启动)。

So models.py of your custom app would be a good place (or any other module which is definitely imported on server startup).

这篇关于django-registration自动创建UserProfile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆