Python / Django django注册添加一个额外的字段

编程入门 行业动态 更新时间:2024-10-23 15:31:50
本文介绍了Python / Django django注册添加一个额外的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经阅读了很多关于如何在使用Django注册时添加一个额外的字段,例如这里,这里和此处。代码片段是: forms.py(从注册应用程序)

I have read a lot about how to add an extra field when using Django-registration, for example here, here and here. The code snippets are: forms.py (from the registration app)

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的注册保存方法,它是默认的后端:

I use the django's registration save method, which is in the default backend:

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

dels.py我创建了一个新类:

In models.py I have created a new class:

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注册应用程序的文件中?

I have not altered anything alse. When I render the form I can see the Institute Field I have added. When I am in the admin site I can see the user profile of each user. However nothing is saved there when I create a new user while the other details of the user are normally saved. I think that I need some kind of configuration but where? Maybe in the files of the Django-registration app?

推荐答案

我已经找出了解决这个问题的方法,我会发布这里有人可以使用它。这很简单,我想。

I have figured out a solution to this problem, and I will post it here in case somebody can use it. It's pretty simple, I suppose.

步骤1 添加一个新模型,这将是一个配置文件,在models.py:

Step 1 Add a new model, which will be a profile, in the 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 创建将使用此模型的表单:

Step 2 Create the form that will use this model:

#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并定义数据的保存方式:

Step 3 Create ragbackend.py and define how the data will be saved:

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并进行以下更改:

Step 4 Go to urls.py and make the following changes:

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')),

请注意,您必须按照该顺序放置网址。很容易理解为什么。

Note that you have to put the URLs in that order. Easy to understand why.

步骤5 现在一切正常,但是您在管理网站上看不到信息。所以你必须去admin.py,然后添加:

Step 5 Now everything works, but you cannot see the information in the admin site. So you have to go to admin.py and add:

#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)

就是这样。不要在 runserver 之前忘记 syncdb 。我希望这会帮助某人。

And that's it. Do not forget the syncdb before runserver. I hope this will help someone.

更多推荐

Python / Django django注册添加一个额外的字段

本文发布于:2023-10-23 02:10:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519431.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   Python   Django   django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!