Django-Registration 在 forms.py 文件中有几个表单类。一种是“类RegistrationFormTermsOfService(RegistrationForm)..
我在 Django 注册代码的其余部分中进行了哪些更改以在我的注册流程中启用此表单而不是 RegistrationForm?
Django-Registration 在 forms.py 文件中有几个表单类。一种是“类RegistrationFormTermsOfService(RegistrationForm)..
我在 Django 注册代码的其余部分中进行了哪些更改以在我的注册流程中启用此表单而不是 RegistrationForm?
更新接受的答案以符合 Django 1.5 和 django-registration 的最新版本:
在 urls.py 中:
from registration.forms import RegistrationFormTermsOfService
from registration.backends.default.views import RegistrationView
urlpatterns = patterns('',
url(r'^accounts/register/$', RegistrationView.as_view(form_class=RegistrationFormTermsOfService), name='registration_register'),
# your other URLconf stuff follows ...
)
然后更新registration_form.html模板并添加一个tos
字段,例如:
<p>
<label for="id_tos">I accept the terms of service</label>
{% if form.tos.errors %}
<p class="errors">{{ form.tos.errors.as_text }}</p>
{% endif %}
{{ form.tos }}
</p>
这是一个使用自定义表单和后端的实际示例,它设置用户名 == 电子邮件地址,并且仅在注册时提示用户输入电子邮件地址。例如my_registration.py
:
from django.conf import settings
from django.contrib.sites.models import RequestSite
from django.contrib.sites.models import Site
from registration import signals
from registration.forms import RegistrationForm
from registration.models import RegistrationProfile
from registration.backends.default import DefaultBackend
class EmailRegistrationForm(RegistrationForm):
def __init__(self, *args, **kwargs):
super(EmailRegistrationForm,self).__init__(*args, **kwargs)
del self.fields['username']
def clean(self):
cleaned_data = super(EmailRegistrationForm,self).clean()
if 'email' in self.cleaned_data:
cleaned_data['username'] = self.cleaned_data['username'] = self.cleaned_data['email']
return cleaned_data
class EmailBackend(DefaultBackend):
def get_form_class(self, request):
return EmailRegistrationForm
在my_registration_urls.py
:
from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template
from registration.views import activate
from registration.views import register
urlpatterns = patterns('',
url(r'^activate/complete/$',
direct_to_template,
{ 'template': 'registration/activation_complete.html' },
name='registration_activation_complete'),
# Activation keys get matched by \w+ instead of the more specific
# [a-fA-F0-9]{40} because a bad activation key should still get to the view;
# that way it can return a sensible "invalid key" message instead of a
# confusing 404.
url(r'^activate/(?P<activation_key>\w+)/$',
activate,
{ 'backend': 'my_registration.EmailBackend' },
name='registration_activate'),
url(r'^register/$',
register,
{ 'backend': 'my_registration.EmailBackend' },
name='registration_register'),
url(r'^register/complete/$',
direct_to_template,
{ 'template': 'registration/registration_complete.html' },
name='registration_complete'),
url(r'^register/closed/$',
direct_to_template,
{ 'template': 'registration/registration_closed.html' },
name='registration_disallowed'),
(r'', include('registration.auth_urls')),
)
然后在你的核心urls.py
中,确保你包括:
url(r'^accounts/', include('my_registration_urls')),
您可以通过执行以下操作简单地进入您urls.py
的表单类并覆盖表单类:
from registration.forms import RegistrationFormTermsOfService
(r'^accounts/register/$', 'registration.views.register', {'form_class' : RegistrationFormTermsOfService}),
您需要在项目的某处编写新的注册表单。如果您只是扩展新字段,则可以继承现有的身份验证表单。然后,您需要编写一个新的后端来处理表单。最后,您需要编写自己的 url 和 auth_urls 并重新定义 url,以通过更改传递给视图的变量来切换视图中的后端和身份验证表单。
打破源代码以查看事情是如何工作的会很有帮助。我的结构基于原始的 django-registration 代码以保持一致。
至于 django 1.11 和 django-registration 2.2 有一些更新的导入......所以如果你得到“No module named 'registration'”这可能是问题......替换:
从registration.backends.hmac.views 导入RegistrationView 来自django_registration.backends.activation.views 导入RegistrationView
从registration.forms 导入RegistrationForm 来自django_registration.forms 导入RegistrationForm
include('django_registration.backends.hmac.urls') 通过 include('django_registration.backends.activation.urls') 在 urls 中
仅举几个... ;)
来源:https ://django-registration.readthedocs.io/en/3.0/custom-user.html