<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _next(self, request, application, roles, next_config):
 """ Continue the state machine at given state. """

        # we only support state changes for POST requests
        if request.method == "POST":
            key = None

            # If next state is a transition, process it
            while True:
                # We do not expect to get a direct state transition here.
                assert next_config['type'] in ['goto', 'transition']

                while next_config['type'] == 'goto':
                    key = next_config['key']
                    next_config = self._config[key]

                instance = load_instance(next_config)

                if not isinstance(instance, Transition):
                    break

                next_config = instance.get_next_config(request, application, roles)

            # lookup next state
            assert key is not None
            state_key = key

            # enter that state
            instance.enter_state(request, application)
            application.state = state_key
            application.save()

            # log details
            log.change(application.application_ptr, "state: %s" % instance.name)

            # redirect to this new state
            url = get_url(request, application, roles)
            return HttpResponseRedirect(url)
        else:
            return HttpResponseBadRequest("<h1>Bad Request</h1>")