Django-Registration在forms.py文件中有几个表单类。其中之一是“class RegistrationFormTermsOfService(RegistrationForm)..
我需要更改Django Registration代码中的哪些部分,才能在我的注册流程中启用此表单,而不是RegistrationForm?
Django-Registration在forms.py文件中有几个表单类。其中之一是“class RegistrationFormTermsOfService(RegistrationForm)..
我需要更改Django Registration代码中的哪些部分,才能在我的注册流程中启用此表单,而不是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>
urls.py
文件,并通过以下方式覆盖表单类:from registration.forms import RegistrationFormTermsOfService
(r'^accounts/register/$', 'registration.views.register', {'form_class' : RegistrationFormTermsOfService}),
这里有一个实际的例子,使用自定义表单和后端设置用户名等于电子邮件地址,并且仅在注册时提示用户输入电子邮件地址。例如,在 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')),
你需要在项目中编写一个新的注册表单。如果你只是扩展新字段,可以继承现有的身份验证表单。然后,你需要编写一个新的后端来处理表单。最后,你需要编写自己的url和auth_urls,并重新定义urls以通过更改传递给视图的变量来切换后端和身份验证表单。
打开源代码以查看其工作方式会很有帮助。我基于原始的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
在urls中包含'django_registration.backends.hmac.urls' 改为包含'django_registration.backends.activation.urls'
只是举几个例子... ;)
来源: https://django-registration.readthedocs.io/en/3.0/custom-user.html
from.registration.backends.simple.views import RegistrationView
。 - Ayed