2010-10-20 36 views
2

我正在使用django-socialregistration来管理我的网站与Facebook的连接。包装第三方Django应用程序的视图时出错? (Facebook,django-socialregistration,django-profiles)

当用户点击“Connect with Facebook”按钮时,我能够自动创建一个新的Django用户并登录他们。但是,我还需要为他们创建一个UserProfile(我的AUTH_PROFILE_MODULE)记录,其中包含他们的Facebook个人资料信息(电子邮件,姓名,地点)。

我相信我需要重写socialregistration的“设置”视图,所以我可以做我需要做的UserProfile。我已经添加了以下到我的项目的urls.py文件:

URL(R '^社会/设置/ $', 'myapp.views.socialreg.pre_setup',名字= 'socialregistration_setup'),

我的自定义视图是在这里 “/myapp/views/socialreg.py”,看起来像:

from socialregistration.forms import UserForm 

def pre_setup(request, template='socialregistration/setup.html', 
       form_class=UserForm, extra_context=dict()): 
    # will add UserProfile storage here... 
    return socialregistration.views.setup(request, template, form_class, extra_context) 

的socialregistration视图签名我重写这个样子的:

def setup(request, template='socialregistration/setup.html', 
      form_class=UserForm, extra_context=dict()): 
    ... 

我收到错误“ViewDoesNotExist at/social/setup /:无法导入myapp.views.socialreg。错误是:没有名为socialregistration.views的模块“当我尝试上述解决方案

社会注册应用工作正常,当我不试图覆盖视图,所以它可能正确地安装在网站包。任何人都知道我在做什么错?

+0

我也在探索使用装饰器,而不是覆盖基于这个SO响应的应用程序视图:http://stackoverflow.com/questions/1649351/overriding-django-views-with-decorators – mitchf 2010-10-20 19:47:18

+0

这看起来很糟糕就像你问的另一个问题http://stackoverflow.com/questions/3982443/how-do-you-wrap-the-view-of-a-3rd-party-django-app。然而,视图没有任何魔力 - 如果找不到模块,它不在你的python路径上。 – tback 2010-10-20 21:57:39

+0

问题简化。 – mitchf 2010-10-21 00:30:08

回答

2

OK,蒂姆指出,这种特殊的问题是相关的路径。

大局观,来完成我想要的东西(创建链接的用户配置时,Django的socialregistration创建方式用户)最好通过将自定义表单传入社会注册的“设置”视图来完成,如作者在此处所建议的:http://github.com/flashingpumpkin/django-socialregistration/issues/issue/36/#comment_482137

截取你的urls.py文件中的相应网址:

from myapp.forms import UserForm  
url('^social/setup/$', 'socialregistration.views.setup', 
    { 'form_class': UserForm }, name='socialregistration_setup'), 
(r'^social/', include('socialregistration.urls')), 

您可以基于用户窗体关闭socialregistration自己的用户窗体,在代码中添加填充并保存用户配置。

+0

请注意,如果您拥有SOCIALREGISTRATION_GENERATE_USERNAME = True,则此功能无法使用 – 2011-05-01 16:44:30