好的,我再次问过。
我这次使用django-registration和后端来帮助我。
如果有人好奇为什么我的regbackend.py有这么多其他功能,那就是服务器抱怨这些方法所以我只是复制并粘贴在那里。
但是,我只想知道后端如何调用将其保存在数据库中的保存功能。
我在这里包含了保存方法,因为我认为它会在这里调用。否则它已经在我的forms.py。
中请帮助我,我实际上已经尝试过查看pinax和django个人资料,但这需要对我的项目进行全面改造。
非常感谢你。
forms.py:
from django import forms
from r2.models import Keyword
from r2.models import UserProfile
from registration.forms import RegistrationForm
from registration.models import RegistrationProfile
from django.utils.translation import ugettext_lazy as _
from registration.forms import RegistrationForm, attrs_dict
class ProjectSpecificRegistrationForm(RegistrationForm):
keywords = forms.ModelChoiceField(queryset=Keyword.objects.all())
first_name =forms.CharField(widget=forms.TextInput(attrs=attrs_dict),label=_(u'First Name'))
last_name =forms.CharField(widget=forms.TextInput(attrs=attrs_dict),label=_(u'Last Name'))
def save(self, profile_callback=None):
new_user = RegistrationProfile.objects.create_inactive_user(username=self.cleaned_data['username'],
password=self.cleaned_data['password1'],
email=self.cleaned_data['email'])
new_profile = UserProfile(user=new_user,username=self.cleaned_data['username'], keywords_subscribed=self.cleaned_data['keywords'],first_name=self.cleaned_data['first_name'],last_name=self.cleaned_data['last_name'])
new_profile.save()
return new_user()
Urls.py:
url(r'^accounts/register/$',register, {'backend': 'registration.regbackend.RegBackend','form_class' : ProjectSpecificRegistrationForm}, name='registration_register'),
url(r'^accounts/', include('registration.backends.default.urls')),
这是我的 regbackend.py:
from django.conf import settings
from django.contrib.sites.models import RequestSite
from django.contrib.sites.models import Site
from django.contrib.auth.models import User
from registration import signals
from registration.forms import RegistrationForm
from registration.models import RegistrationProfile
from r2.forms import ProjectSpecificRegistrationForm
from r2.models import *
class RegBackend(object):
def register(self, request, **kwargs):
username, email, password = kwargs['username'], kwargs['email'], kwargs['password1']
if Site._meta.installed:
site = Site.objects.get_current()
else:
site = RequestSite(request)
new_user = RegistrationProfile.objects.create_inactive_user(username, email,
password, site)
user = User.objects.get(username=new_user.username)
user.first_name = kwargs['first_name']
user.last_name = kwargs['last_name']
user.keywords = kwargs['keywords']
signals.user_registered.send(sender=self.__class__, user=new_user, request=request)
user.save()
return new_user
def save(self, profile_callback=None):
print('Came in')
new_user = RegistrationProfile.objects.create_inactive_user(username=self.cleaned_data['username'],
password=self.cleaned_data['password1'],
email=self.cleaned_data['email'])
new_profile = UserProfile(user=new_user,username=self.cleaned_data['username'], keywords_subscribed=self.cleaned_data['keywords'],first_name=self.cleaned_data['first_name'],last_name=self.cleaned_data['last_name'])
new_profile.save()
return new_user()
def registration_allowed(self, request):
return getattr(settings, 'REGISTRATION_OPEN', True)
def post_registration_redirect(self, request, user):
return ('registration_complete', (), {})
答案 0 :(得分:1)
要创建新用户,您应该使用后端的register
方法。您需要在request
处传递,因此应该从视图(或其他可以访问request
的地方)调用
backend = RegBackend()
user = backend.register(request, **your_kwargs)
服务器抱怨这些方法所以我只是复制并粘贴在那里。
无需复制任何内容,只需使用继承(来自registration.backends.default.DefaultBackend
)
注册后端没有(也不应该)有save
方法。