1

说到 django,我还是个新手,但我已经安装了 django-profiles 和 django-registration,并运行了最新的 django 1.5.1。我还阅读了一些指南,即http://dmitko.ru/django-registration-form-custom-field/http://birdhouse.org/blog/2009/06/27/django-profiles/Django-Registration & Django-Profile,使用你自己的自定义表单

我没有自定义 django-registration,但我尝试制作自己的配置文件类,看起来像这样:

from django.contrib.auth.models import User
class UsrProfile(models.Model):
    user = models.ForeignKey(User, unique=True, primary_key=True)
    ...custom fields here...

但我知道可以让用户注册,但拥有个人资料是另一回事,因为用户无需创建个人资料即可注册。

我的问题是,我怎样才能坚持我制作的个人资料课程,进入注册?我想强制用户在注册时填写我在个人资料类中指定的详细信息...

我已经尝试在这里和那里遵循一些教程/指南,但是每当他们说将 urls.py 修改为以下内容时,我也会一直卡住:

url(r'^accounts/register/$',
    register,
    {'backend': ... form_class...
    ),

因为每当我尝试这个时,似乎无法识别“注册”(在第二行),我也不知道它指的是什么......我尝试类似:

from registration.views import register

但它似乎没有将注册识别为有效的导入......

4

2 回答 2

0

您可以扩展类用户,并使用您想要的信息创建表单。

扩展用户:models.py

User.add_to_class('phone', models.CharField(max_length=12))
User.add_to_class('books', models.ManyToManyField(Func,null=True,blank=True))

表格.py

class AddUserForm(forms.Form):
    username = forms.CharField(label="Rut",widget=forms.TextInput())
    email    = forms.EmailField(label="Correo Electronico",widget=forms.TextInput())
    password = forms.CharField(label="Password",widget=forms.PasswordInput(render_value=False))
    phone = forms.CharField(label="Telefono",widget=forms.TextInput())
    books = forms.ModelMultipleChoiceField(queryset=Book.objects.all(),label='Books')

def clean_username(self):
    username = self.cleaned_data['username']
    try:
        u = User.objects.get(username=username)
    except User.DoesNotExist:
        return username
    raise forms.ValidationError('Username is ready')

def clean_email(self):
    email = self.cleaned_data['email']
    try:
        u = User.objects.get(email=email)
    except User.DoesNotExist:
        return email
    raise forms.ValidationError('Email is ready')

视图.py

def registerUser(request):
        form = AddUserForm()
    if request.method == "POST":
        form = AddUserForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            email = form.cleaned_data['email']
            password = form.cleaned_data['password']
            u = User.objects.create_user(username=username,email=email,password=password)
            u.phone = form.cleaned_data['phone']
            u.books = form.cleaned_data['books']
            u.save() 
            return HttpResponseRedirect('/users/')
        else:
            ctx = {'form':form}
            return      render_to_response('register.html',ctx,context_instance=RequestContext(request))

    ctx = {'form':form}
    return render_to_response('register.html',ctx,context_instance=RequestContext(request))
于 2013-05-27T03:42:11.137 回答
0

一个解法 :

在您的自定义 url.py

from registration.backends.default.views import RegistrationView
url(r'^register/$',
   RegistrationView.as_view(form_class=YourCustomForm),
   name='registration_register'),
),

代替 :

from registration.views import register
url(r'^accounts/register/$',
    register,
    {'backend': ... form_class...
),

说明:

查看文档

'form_class' 参数:

“用于用户注册的表单类。可以在每个请求的基础上被覆盖(参见>下文)。应该是实际的类对象。”

因此,'YourCustomForm' 是从 RegistrationForm 类扩展而来的表单,如下所示:

class UserRegistrationForm(RegistrationForm):
    lastname = forms.CharField(widget=forms.TextInput(attrs=attrs_dict))
    ...
于 2013-12-10T15:40:36.277 回答