当谈到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, using your own custom form
我没有自定义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
但它似乎并不认为注册为有效的导入......
答案 0 :(得分: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))
Form.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')
view.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))
答案 1 :(得分:0)
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))
...