2

I'm new to django, working on a website requiring multiple types of registrations, needing two seperate forms, and basing myself off of cookiecutter-django's template, which utilizes allauth.

I'd still like to retain allauth's email confirmation and password verification.

I've searched around for a solution, coming down to this: Multiple signup, registration forms using django-allauth

It doesn't resolve my problem. The sign up form stays the same in my case. Perhaps only because I'm trying to use the model in the form?

Here's my attempt at a customized log on form:

in cookiecutter's app users/

In models.py:

class Proveedor(models.Model):
    TIENE_EMPRESA = (
        ('S','Si'),
        ('M','No'),
    )
    SEXO = (
        ('F','Femenina'),
        ('M','Masculino'),
    )
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    oficios = []
    ubicacion = models.CharField(_('Ubicacion'),max_length=255)
    ubicaciones_servicios = []
    tiene_empresa = models.CharField(_('Tienes Empresa?'),max_length=1, choices=TIENE_EMPRESA)
    foto = models.ImageField(_('Foto'),upload_to=None, blank=False)
    email = models.EmailField(_('Email'),max_length=254)
    fecha_nacimiento = models.DateField(_('Fecha de nacimiento'), default=datetime.datetime.now(), blank=True)
    sexo = models.CharField(_('Sexo'),max_length=1, choices=SEXO)
    telefono = models.CharField(_('Numero de Telefono'),max_length=10)

    fotos_trabajos_realizados = []
    descripcion_forma_trabajar = models.TextField(_('Descripcion de Forma de Trabajar'),null=True, blank=True)

    class Meta:
        db_table = 'proveedor'
        permissions = (
            ("menu_proveedor", "Puede ver menu de proveedor"),
    )

In formularios.py (forms):

class FormularioProveedor(SignupForm):
    class Meta:
        model = Proveedor
        fields = '__all__'

def save(self, request, user):
    user = super(FormularioProveedor, self).save(request)
    user.sexo = self.cleaned_data['sexo']
    user.tiene_empresa = self.cleaned_data['tiene_empresa']
    user.save()

In views.py:

class ProveedorRegistroView(SignupView):
    template_name = 'account/form_proveedor.html'
    form_class = FormularioProveedor
    redirect_field_name = 'proximo'
    view_name = 'registroproveedor'
    success_url = None

def get_context_data(self, **kwargs):
    ret = super(ProveedorRegistroView, self).get_context_data(**kwargs)
    ret.update(self.kwargs)
    return ret

registroproveedor = ProveedorRegistroView.as_view()

urls.py in the package directory for testing:

url(r'registroproveedor', registroproveedor, name='registroproveedor')

Finally the template itself:

{% extends "account/base.html" %}

{% load i18n %}
{% load crispy_forms_tags %}

{% block title %}{% trans "Registro de Proveedor" %}{% endblock title %}

{% block content %}
<form id="signup_form" method="post" action="{% url 'registroproveedor'  %}">
  {% csrf_token %}
  {{ form|crispy }}
</form>
{% endblock %}

I've only had new fields show up by pointing to my form in the settings, assigning ACCOUNT_SIGNUP_FORM_CLASS ... but that's not really what I need or want. I just want multiple registration forms, still able to utilize features provided by allauth and the template provided by cookiecutter-django. Right now, basing myself off of this template, I see too many things that might be wrong and any help would be appreciated. Thanks.

Community
  • 1
  • 1
mitbanip
  • 179
  • 3
  • 13

1 Answers1

1

When I was learning more about Django's default user registration forms I came across the site below:

https://learndjango.com/tutorials/django-custom-user-model

One key aspect of this tutorial was using the default Django class models to create new class models that are more flexible if one wanted to add new meta fields. In the cookiecutter-django template they do this as following in the .../users/forms.py file:

from django.contrib.auth import forms as admin_forms

class UserCreationForm(admin_forms.UserCreationForm): 
    ...

As can be seen above the import the default Django class model and use it to create a class model that inherits all the methods and properties of the default class. If I understand your question correctly you want to use cookiecutter-django default templates and reuse them multiple times to create different sorts of registration forms.

I might be wrong, but the following might work:

from (DIRECTORY, example: ...user/forms.py) import (COOKIECUTTER-DJANGO REGISTRATION CLASS, example UserCreationForm

class NewUserRegistrationForm(UserCreationForm):
    

    Class Meta(UserCreationForm.Meta):
        model = User # cookiecutter-django template already includes this
        fields = ('username', 'email', 'street', 'house_number', 'postal_code', 'province', 'state', 'country',)
        # add additional fields in here

Please note, the cookiecutter-django UserCreationForm is already a copy of the default Django UserCreationForm and thus the code I showed in the example is a copy of the cookiecutter-django UserCreationForm. Thus the example is no different from the default Django UserCreationForm with the exception of the included Meta fields variable and the utilization of allauth.

I'm still a novice myself so I could be wrong, but this might solve your problem.