0
votes

I want to make a registration page for my django application in order to 'request' access. In other words they submit the form and it creates the user and user.is_active = False. Then as admin I can approve their access.

However I do not want them to be able to put in their username. Instead i want their username to be firstname.lastname. In the unlikely case of the same name people trying to sign up (app is for small number of people) - then a number will need adding on the end.

I do this at the moment using a signup view:

def signup(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)

        if form.is_valid():
            form.save()

            first_name = form.cleaned_data.get('first_name')
            last_name = form.cleaned_data.get('last_name')
            raw_password = form.cleaned_data.get('password1')
            username = form.cleaned_data.get('username')

            user = authenticate(username=username, password=raw_password)

            user.is_active = False
            user.save()
            return render(request, 'registration/signedup.html', {'user': user})
        else:
            return render(request, 'registration/signup.html', {'form': form, 'invalid': 'Please try again.'})
    else:
        form = SignUpForm()
        return render(request, 'registration/signup.html', {'form': form})

and a signup form:

class SignUpForm(UserCreationForm):
    first_name = forms.CharField(max_length=30, required=False, help_text='Optional.')
    last_name = forms.CharField(max_length=30, required=False, help_text='Optional.')
    email = forms.EmailField(max_length=254, help_text='Required. Inform a valid email address.')

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

and this as the html for my signup page:

<form method="post"  autocomplete="off">
  {% csrf_token %}


   <table class='login' width='400'>
   {% for field in form %}

   <tr>
     <td width='200' class='loginrow'>{{ field.label_tag }}</td>
     <td width='180' class='loginrow'>{{ field }}</td>
       {% for error in field.errors %}
      <p style="color: red">{{ error }}</p>
       {% endfor %}
   </tr>
   {% endfor %}
</table>

 <button type="submit">Sign up</button>

</form>

I have tried to remove username from my form field, and then create username variable as firs_name+'.'+last_name in my view by the user = authenticate() returns none.

Is there a way I can automatically produce a username without the user submitting one? - I was also thinking of using javascript function in the html page, and a hidden 'username' input tag which concats the names, but I can't get this to work either.

Thanks

2

2 Answers

1
votes

Override save method in SignUpForm

def save(self, commit=True):
    instance = super(SignUpForm, self).save(commit=False)
    instance.username = "%s.%s" %(self.cleaned_data['first_name'], self.cleaned_data['last_name'])
    if commit:
        instance.save()
    return instance
0
votes

You could try something like:

my_data = dict(request.POST.iterlists())
my_data['username'] = my_data['firstname'] + '.' + my_data['lastname']

form = SignUpForm(my_data)

if form.is_valid():
            form.save()
...