B
B
BogBel2015-11-18 21:05:13
Django
BogBel, 2015-11-18 21:05:13

NotImplementedError, Adding custom fields to django-registration?

Good evening. I already asked here about adding two fields to the standard django-registration (not redux) form. Essence of the question: it is necessary to give the user on a level with username , password , email to enter available in the standard User class last_name, first_name.
And so it was done:
forms.py

from registration.forms import RegistrationForm
from django.forms.fields import CharField
from django.contrib.auth.models import User


class UpdatedRegistrationForm(RegistrationForm):
    first_name = CharField(max_length=20)
    last_name = CharField(max_length=30)

    class Meta:
        model = User
        fields = ('username', 'email', 'password1', 'password2')

    def save(self, commit = True):
        user = super(UpdatedRegistrationForm, self).save(commit=False)
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        if commit:
            user.save()
            return user

urls.py
from django.conf.urls import include, url
from django.contrib import admin
from accounts.views import home_page, admin_panel
from accounts.forms import UpdatedRegistrationForm
from registration.views import RegistrationView


class RegistrationViewUniqueEmail(RegistrationView):
    form_class = UpdatedRegistrationForm

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^home/', home_page),
    url(r'^admin_pannel/',admin_panel),
    url(r'^register/$', RegistrationViewUniqueEmail.as_view(), name='registration_register'),
    url(r'', include('registration.backends.simple.urls')),
    url(r'', include('social_auth.urls')),


]

When starting the server and going to /register/, now, in addition to the standard username, password1, password2, email , the desired first_name , last_name
247ce70e83004b4dacde46bcd6504b01.png
are also displayed. After that, we send the data and get the following:
61a415d0ae844b5188ce373803057eb7.png
cd2a7d7ab75245129cf27ecbb6343ff7.png
But the data is sent, this is visible in the request body
58d6db2eac1146a2875f74fa0b23aa64.png
As a hypothesis, I assume that it is possible the problem is not overridden __init__() for my form. But this is just a hypothesis. I would be grateful for the corrections, and for practical advice.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rostislav Grigoriev, 2015-11-19
@crazyzubr

There is clearly an error here.

if commit:
    user.save()
    return user

Need like this:
if commit:
    user.save()
return user

And the reason for the error on the screenshots is that you need to import it into urls.py
from registration.backends.default.views import RegistrationView
but notfrom registration.views import RegistrationView

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question