[Answered ]-Django how to reverse to autocreated page

1👍

You can work with:

class AddCompanyEmployee(CreateView):
    model = CustomUser
    template_name = 'manage/add_employee.html'
    form_class = AddCompanyEmployeeForm

    def get_success_url(self):
        return reverse(
            'userprofile_detail', kwargs={'pk': self.object.userprofile.pk}
        )

It will look for the OneToOneField in reverse, and thus obtain the .pk of the related UserProfile.

That being said it is quite strange that you use both a CustomUser and UserProfile. Usually if you implement your own user model, that is to add fields that you would otherwise store in a UserProfile, and thus to prevent having to work with two models.

Leave a comment