7

I have been reading a lot of questions and answers regarding this, but still no good luck. For example here is a great answer but most probably doesn't apply to django-registration 1.0.

My goal is to add two custom fields, namely organization and position in the sign up form. Note: I am using one-step django registration provided by registration.backend.simple.

Community
  • 1
  • 1
Iqbal
  • 2,094
  • 1
  • 20
  • 28
  • why do you say the answer does not apply to django-registration 1.0? – Chris Hawkes Oct 29 '14 at 14:40
  • @ChrisHawkes I am getting a "cannot import name register" error. I think register is deprived in django-registration 1.0. And I tried using "url(r'^register/$', RegistrationView.as_view(form_class=UserRegForm))". But still no luck – Iqbal Oct 29 '14 at 14:47
  • according to one of the answers provided to fix that error with the new version of django-registration you have to add this, from django.conf.urls import patterns, include, url from registration.backends.default.views import RegistrationView from abby.apps.accounts.forms import UserRegForm urlpatterns = patterns('', url(r'^register/$', RegistrationView.as_view(form_class=UserRegForm)), ) http://stackoverflow.com/questions/14726725/python-django-django-registration-add-an-extra-field/16366997#16366997 – Chris Hawkes Oct 29 '14 at 15:14
  • @ChrisHawkes I am sorry to say that is not working either. I already mentioned in my previous comment that I had tried that. – Iqbal Oct 29 '14 at 16:02
  • @Iqbal If you still have the issue, please edit your description to include the exception or problem after trying Chris suggestion. Or else if you solved the problem, post the solution to help other readers with a similar issue – tutuDajuju Mar 07 '15 at 08:59

1 Answers1

0

Since you don't have an answer yet, I'm offering this one, although it's not exactly what you are asking for. I think it might help you anyway, though...

This is how I have done it in a few projects using django-registration 1.0, Python 2.7 and Django 1.6. In this case, I just use username, email and password for sign up and then the user can add profile fields after they have registered. It shouldn't be too tough to modify this to accept the fields upon registration.

I use Twitter Bootstrap for styling, so my templates may or may not help you. I left them out in this case.

I created a couple apps called accounts and authentication that hold my models, forms and views:

accounts.models.UserProfile

from django.db import models

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    title = models.CharField(max_length=100)
    company = models.CharField(max_length=250)

authentiction.forms.EditUserProfileForm

from django.forms import ModelForm

class EditUserProfileForm(ModelForm):**
    . . .
    title = forms.CharField(widget=forms.TextInput())
    company = forms.CharField(widget=forms.TextInput())
    . . .

accounts.views.EditUserProfileView

from django.views.generic.base import View
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponseRedirect

from .models import UserProfile
from .forms import EditUserProfileForm

class EditUserProfileView(View):

    form_class = EditUserProfileForm
    template_name = 'accounts/profile_edit.html'

    @method_decorator(login_required)
    def get(self, request, *args, **kwargs):

        profile = get_object_or_404(UserProfile, user=request.user)

        form_dict = {
            'title': profile.title,
            'company': profile.company,
        }

        form = self.form_class(form_dict)

        return render(request, self.template_name, {
            'form': form,
            'profile': profile,
        })

    @method_decorator(login_required)
    def post(self, request, *args, **kwargs):

        profile = get_object_or_404(UserProfile, user=request.user)

        form = self.form_class(request.POST, instance=profile)

        if form.is_valid():

            company = form.cleaned_data['company']
            title = form.cleaned_data['title']

            title.company = title
            profile.company = company

            # you might need to save user too, depending on what fields
            request.user.save()
            profile.save()

            return HttpResponseRedirect('/dashboard/')

        return render(request, self.template_name, {'form': form})

project.urls

url(r'^accounts/profile/edit/', EditUserProfileView.as_view(),  name='edit_user_profile_view'),
nicorellius
  • 3,715
  • 4
  • 48
  • 79