我已经阅读了很多关于如何在使用Django-registration时添加额外字段的内容,例如here,here和here。代码片段是: forms.py(来自注册应用程序)
class RegistrationForm(forms.Form):
username = forms.RegexField(regex=r'^\w+$', max_length=30, widget=forms.TextInput(attrs=attrs_dict), label=_("Username"), error_messages={ 'invalid': _("This value must contain only letters, numbers and underscores.") })
email = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=75)), label=_("Email"))
password1=forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False), label=_("Password"))
institute=forms.CharField(max_length=50) #This is the new!
password2 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False), label=_("Password (again)"))
captcha = CaptchaField()
我使用django的注册保存方法,该方法位于默认后端:
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)
signals.user_registered.send(sender=self.__class__,
user=new_user,
request=request)
return new_user
在models.py中,我创建了一个新类:
class UserProfile(models.Model):
user = models.OneToOneField(User)
institute=models.CharField(max_length=50)
def create_user_profile(sender, instance, created, **kwargs):
if created:
profile, created = UserProfile.objects.get_or_create(user=instance)
post_save.connect(create_user_profile, sender=User)
在admin.py中:
admin.site.unregister(User)
class UserProfileInline(admin.StackedInline):
model = UserProfile
class UserProfileAdmin(UserAdmin):
inlines = [ UserProfileInline, ]
admin.site.register(User, UserProfileAdmin)
我没有改变任何东西。当我渲染表格时,我可以看到我添加的研究所字段。当我在管理站点时,我可以看到每个用户的用户配置文件。但是,当我创建新用户时,通常保存用户的其他详细信息时,不保存任何内容。我认为我需要某种配置,但在哪里?也许在Django注册应用程序的文件中?
答案 0 :(得分:15)
我已经找到了解决这个问题的方法,我会在这里发布,以防有人可以使用它。我想这很简单。
第1步在models.py中添加一个新模型,该模型将是一个配置文件:
#models.py
class user_profile(models.Model):
user=models.ForeignKey(User, unique=True)
institution=models.CharField(max_length=200)
def __unicode__(self):
return u'%s %s' % (self.user, self.institution)
第2步:创建将使用此模型的表单:
#forms.py
from registration.forms import RegistrationForm
from django.forms import ModelForm
from Test.models import user_profile
class UserRegForm(RegistrationForm):
institution=forms.CharField(max_length=200)
第3步:创建ragbackend.py并定义数据的保存方式:
from Test.models import user_profile
from forms import *
def user_created(sender, user, request, **kwargs):
form = UserRegForm(request.POST)
data = user_profile(user=user)
data.institution = form.data["institution"]
data.save()
from registration.signals import user_registered
user_registered.connect(user_created)
第4步:转到urls.py并进行以下更改:
import regbackend
from registration.views import register
url(r'^accounts/register/$', register, {'backend': 'registration.backends.default.DefaultBackend','form_class': UserRegForm}, name='registration_register'),
(r'^accounts/', include('registration.urls')),
请注意,您必须按此顺序放置网址。容易理解为什么。
第5步现在一切正常,但您无法在管理网站中看到这些信息。所以你必须去admin.py并添加:
#admin.py
from django.contrib.auth.models import User
from Test.models import user_profile
admin.site.unregister(User)
class UserProfileInline(admin.StackedInline):
model = user_profile
class UserProfileAdmin(UserAdmin):
inlines = [ UserProfileInline, ]
admin.site.register(User, UserProfileAdmin)
就是这样。不要忘记syncdb
之前的runserver
。我希望这会对某人有所帮助。
答案 1 :(得分:5)
除了Garrett的回答,我在django-registration版本0.8中收到了“无法导入名称注册”错误。我认为这是因为基于类的视图,所以相应地更新urls.py
:
from django.conf.urls import patterns, include, url
from registration.backends.default.views import RegistrationView
from abby.apps.accounts.forms import UserRegForm
urlpatterns = patterns('',
url(r'^register/$', RegistrationView.as_view(form_class=UserRegForm)),
)