如何在Django-registration 1.0中添加额外(自定义)字段

7
我一直在阅读关于这个问题的问题和答案,但仍然没有好运气。例如,在这里有一个很好的答案,但很可能不适用于django-registration 1.0。
我的目标是在注册表单中添加两个自定义字段,即“组织”和“职位”。 注意:我正在使用由registration.backend.simple提供的一步式django注册。

为什么你说这个答案不适用于 Django-registration 1.0? - Chris Hawkes
@ChrisHawkes 我遇到了一个“无法导入名称register”的错误。我认为在django-registration 1.0中取消了register。我尝试使用“url(r'^register/$', RegistrationView.as_view(form_class=UserRegForm))”但仍然没有成功。 - Iqbal
根据提供的答案之一,为了修复django-registration的新版本中的错误,您需要添加以下内容:从django.conf.urls导入模式、包括、url 从registration.backends.default.views导入RegistrationView 从abby.apps.accounts.forms导入UserRegFormurlpatterns = patterns('', url(r'^register/$', RegistrationView.as_view(form_class=UserRegForm)), )https://dev59.com/rm7Xa4cB1Zd3GeqPmSFU#16366997 - Chris Hawkes
@ChrisHawkes 很抱歉告诉你,那个也不起作用。我在之前的评论中已经提到过我尝试过了。 - Iqbal
@Iqbal 如果您仍然遇到问题,请在尝试Chris的建议后编辑您的描述,包括异常或问题。否则,如果您已经解决了问题,请发布解决方案以帮助其他遇到类似问题的读者。 - tutuDajuju
1个回答

0

由于您还没有答案,我提供这个答案,虽然它不完全是您所要求的。但我认为它可能会对您有所帮助...

这是我在使用django-registration 1.0、Python 2.7和Django 1.6时在几个项目中完成的。在这种情况下,我只使用用户名电子邮件密码进行注册,然后用户可以在注册后添加配置文件字段。不过,将其修改为在注册时接受这些字段应该不太困难。

我使用Twitter Bootstrap进行样式设计,因此我的模板可能对您有所帮助。但在这种情况下,我将它们省略了。

我创建了两个名为账户身份验证的应用程序,其中包含我的模型、表单和视图:

accounts.models.UserProfile

from django.db import models

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    title = models.CharField(max_length=100)
    company = models.CharField(max_length=250)

authentiction.forms.EditUserProfileForm

from django.forms import ModelForm

class EditUserProfileForm(ModelForm):**
    . . .
    title = forms.CharField(widget=forms.TextInput())
    company = forms.CharField(widget=forms.TextInput())
    . . .

accounts.views.EditUserProfileView

from django.views.generic.base import View
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponseRedirect

from .models import UserProfile
from .forms import EditUserProfileForm

class EditUserProfileView(View):

    form_class = EditUserProfileForm
    template_name = 'accounts/profile_edit.html'

    @method_decorator(login_required)
    def get(self, request, *args, **kwargs):

        profile = get_object_or_404(UserProfile, user=request.user)

        form_dict = {
            'title': profile.title,
            'company': profile.company,
        }

        form = self.form_class(form_dict)

        return render(request, self.template_name, {
            'form': form,
            'profile': profile,
        })

    @method_decorator(login_required)
    def post(self, request, *args, **kwargs):

        profile = get_object_or_404(UserProfile, user=request.user)

        form = self.form_class(request.POST, instance=profile)

        if form.is_valid():

            company = form.cleaned_data['company']
            title = form.cleaned_data['title']

            title.company = title
            profile.company = company

            # you might need to save user too, depending on what fields
            request.user.save()
            profile.save()

            return HttpResponseRedirect('/dashboard/')

        return render(request, self.template_name, {'form': form})

project.urls

url(r'^accounts/profile/edit/', EditUserProfileView.as_view(),  name='edit_user_profile_view'),

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接