1

I'm still a newbie when it comes to django, but I've installed both django-profiles and django-registration, and running the latest django 1.5.1. I've also read a few guides, namely http://dmitko.ru/django-registration-form-custom-field/, http://birdhouse.org/blog/2009/06/27/django-profiles/, Django-Registration & Django-Profile, using your own custom form

I've not customized django-registration but I've tried to make my own profiles class that looks something like this:

from django.contrib.auth.models import User
class UsrProfile(models.Model):
    user = models.ForeignKey(User, unique=True, primary_key=True)
    ...custom fields here...

But I understand that it's possible to have users register but it's a separate thing to have profiles, in that users can register without having to create a profile.

My question is, how can I stick in the profiles class I made, into the registration? I want to force users to fill out details I specified in my profiles class when they are registering...

I've tried following some tutorials/guides here and there but I also keep getting stuck whenever they say to modify the urls.py to something like:

url(r'^accounts/register/$',
    register,
    {'backend': ... form_class...
    ),

because whenever I try this, the 'register' (in the second line) doesn't seem to be recognized and I have no idea what it's referring to either....I try something like:

from registration.views import register

but it doesn't seem to recognize register as a valid import...

Community
  • 1
  • 1
Stylex
  • 73
  • 8
  • what is the error it throws? –  May 26 '13 at 20:48
  • NameError at /accounts/register name 'register' is not defined – Stylex May 26 '13 at 21:51
  • Ok, I /think/ I've fixed the import error stuff but I'm still having trouble figuring out what's the right way to go about all this. That docs.b-list.org site has been down for a long time as well. I think the confusion for me also sprouts from the fact that previous versions implemented things differently than the current/latest versions out there. All I'm trying to do is stick in the fields from profiles that I've already made, into the /accounts/register/ of django-registration. I feel like this is a somewhat trivial problem thats been encountered before but I can't seem to find much on it. – Stylex May 26 '13 at 22:53
  • Although the error in my code has gone away, when I navigate to /accounts/register/ I get: TypeError at /accounts/register/ unbound method register() must be called with RegistrationView instance as first argument (got WSGIRequest instance instead) – Stylex May 26 '13 at 23:33

2 Answers2

0

you could extend the class user, and create the form with the information that you want.

for extend User: models.py

User.add_to_class('phone', models.CharField(max_length=12))
User.add_to_class('books', models.ManyToManyField(Func,null=True,blank=True))

Form.py

class AddUserForm(forms.Form):
    username = forms.CharField(label="Rut",widget=forms.TextInput())
    email    = forms.EmailField(label="Correo Electronico",widget=forms.TextInput())
    password = forms.CharField(label="Password",widget=forms.PasswordInput(render_value=False))
    phone = forms.CharField(label="Telefono",widget=forms.TextInput())
    books = forms.ModelMultipleChoiceField(queryset=Book.objects.all(),label='Books')

def clean_username(self):
    username = self.cleaned_data['username']
    try:
        u = User.objects.get(username=username)
    except User.DoesNotExist:
        return username
    raise forms.ValidationError('Username is ready')

def clean_email(self):
    email = self.cleaned_data['email']
    try:
        u = User.objects.get(email=email)
    except User.DoesNotExist:
        return email
    raise forms.ValidationError('Email is ready')

view.py

def registerUser(request):
        form = AddUserForm()
    if request.method == "POST":
        form = AddUserForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            email = form.cleaned_data['email']
            password = form.cleaned_data['password']
            u = User.objects.create_user(username=username,email=email,password=password)
            u.phone = form.cleaned_data['phone']
            u.books = form.cleaned_data['books']
            u.save() 
            return HttpResponseRedirect('/users/')
        else:
            ctx = {'form':form}
            return      render_to_response('register.html',ctx,context_instance=RequestContext(request))

    ctx = {'form':form}
    return render_to_response('register.html',ctx,context_instance=RequestContext(request))
Oztro
  • 86
  • 1
  • 12
0

A Solution :

In your custom url.py

from registration.backends.default.views import RegistrationView
url(r'^register/$',
   RegistrationView.as_view(form_class=YourCustomForm),
   name='registration_register'),
),

Instead of :

from registration.views import register
url(r'^accounts/register/$',
    register,
    {'backend': ... form_class...
),

Explications :

Get a look on the documentation.

'form_class' parameter :

"The form class to use for user registration. Can be overridden on a per-request basis (see >below). Should be the actual class object."

So 'YourCustomForm' is a form extended from the RegistrationForm Class like this :

class UserRegistrationForm(RegistrationForm):
    lastname = forms.CharField(widget=forms.TextInput(attrs=attrs_dict))
    ...
1ronmat
  • 1,147
  • 8
  • 15