यहां एक कस्टम प्रपत्र और बैकएंड का उपयोग करके एक व्यावहारिक उदाहरण है जो उपयोगकर्ता नाम == ईमेल पता सेट करता है, और केवल उपयोगकर्ता को पंजीकरण पर ईमेल पते के लिए संकेत देता है। में, उदाहरण के लिए 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')),
जो आश्चर्यजनक रूप से सरल लगता है ... और नया फॉर्म reg फ़ॉर्म के पुनः लोड पर दिखाना चाहिए? – Brenden
आप इसे क्यों न दें :-) (संकेत: हाँ, यह चाहिए) –
तो मैं इसे अपने मुख्य urls.py में और पंजीकरण लाइन से पहले डाल रहा हूं। लेकिन मुझे त्रुटि रजिस्टर मिल रहा है() कम से कम 2 गैर-कीवर्ड तर्क (1 दिया गया) – Brenden