Django-Registration在forms.py文件中有几个表单类。一个是“类RegistrationFormTermsOfService(RegistrationForm)..
我在Django注册代码的其余部分更改了什么以在我的注册流程中启用此表单而不是RegistrationForm?
答案 0 :(得分:13)
更新已接受的答案以符合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>
答案 1 :(得分:5)
以下是使用自定义表单和后端设置username ==电子邮件地址的实际示例,并且仅在注册时提示用户输入电子邮件地址。在,例如, 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')),
答案 2 :(得分:5)
您可以直接进入urls.py
并通过执行以下操作覆盖表单类:
from registration.forms import RegistrationFormTermsOfService
(r'^accounts/register/$', 'registration.views.register', {'form_class' : RegistrationFormTermsOfService}),
答案 3 :(得分:1)
您需要在项目的某个位置编写新的注册表单。如果您只是扩展新字段,则可以继承现有的身份验证表单。然后,您将要编写一个新的后端来处理表单。最后,您需要编写自己的url和auth_urls,并通过更改传递给视图的变量来重新定义URL以在视图中切换后端和身份验证表单。
打开源以查看工作原理是很有帮助的。我的结构基于原始的django注册代码,以保持一致。
答案 4 :(得分:0)
对于django 1.11和django-registration 2.2,有一些更新的导入...因此,如果您得到“没有名为'registration'的模块”,则可能是问题所在... 替换:
从registration.backends.hmac.views导入RegistrationView 通过从django_registration.backends.activation.views导入RegistrationView
导入RegistrationForm 由django_registration.forms导入RegistrationForm
include('django_registration.backends.hmac.urls') 通过include('django_registration.backends.activation.urls')
仅举几例...;)
Src:https://django-registration.readthedocs.io/en/3.0/custom-user.html